Solved

C++ Object Declaration - What's the difference between Object* and Object& and Object.

Posted on 2006-10-27
1
877 Views
Last Modified: 2012-06-27
Hello,
In C++, as far as I know, there are three ways to declare an object.

MyClass myObject;  /* calls the constructor of MyClass to create an instance of myObject */
MyClass *myObject; /* does the same as above. So what's the difference? */
MyClass &myObject; /* does the same. So what's the difference? */

My questions:

1. *myObject means it points to myObject.
So my question here is, does it do the same thing as "MyClass myObject" and then it just points to it?
And when you want to call a method of MyClass you do "myObject->callThisMethod()"?
Is that the only difference?
Is memory allocated on the free store when you do  MyClass *myObject?


2. &myObject is my biggest problem. It says myObject is a Reference. <-- what does it mean when you say it "is a reference"?
What is the difference between "&myObject" and just "myObject"?
In both cases, you can do "myObject.callThisMethod()" so I don't see any difference when using them.
When you do "MyClass &myObject", is memory allocated on the free store?


Thanks.

0
Comment
Question by:blast010
1 Comment
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 17824432
> 1. *myObject means it points to myObject.
> So my question here is, does it do the same thing as "MyClass myObject" and then it just points to it?

No, it just declares a pointer with uninitialized content.  It does not create anything other than the pointer.

> And when you want to call a method of MyClass you do "myObject->callThisMethod()"?

That's the right syntax, but this will only work if the pointer myObject actually points to a MyClass.  The language doesn't guarantee that it will--that's the programmer's responsibility.

> Is that the only difference?

No, see above.

> Is memory allocated on the free store when you do  MyClass *myObject?

No.

> 2. &myObject is my biggest problem. It says myObject is a Reference. <-- what does it mean when you say it "is a reference"?

You can think of it as "myObject" being a name for some MyClass object somewhere.  Or you can think of it as being like a constant pointer that is automatically dereferenced.

A reference always has to refer to some object, so you couldn't have a declaration like you showed.  The compiler should complain that the reference has to refer to some object.

> What is the difference between "&myObject" and just "myObject"?

It depends on the context.  In a declaration like the ones you showed, the first one declares a reference and the second one declares an object.  The initial & can also be the address-of operator in other contexts, so if myObject were the name of a real object, &myObject would be a pointer to that object.

> In both cases, you can do "myObject.callThisMethod()" so I don't see any difference when using them.

It can be convenient to give an object another name.  For example, say you have some code that uses yourHouse.upstairs.guestBedroom.door.doorknob.  To make the code simpler and more readable, you can declare

Doorknob& theKnob = yourHouse.upstairs.guestBedroom.door.doorknob;

Then in the following code, you can just refer to theKnob instead of the long qualified expression.

References are also useful as function parameters.  If you pass an object by value, you incur the expense of copying it and the called function can't do anything to the original object.  If you pass a reference, you avoid the copy and the called function can change the original object.  If you don't want the called function to be able to change the original object, you can declare the parameter to be a reference to a const object, and then the compiler will complain if the function tries to change it.

All this could be done with pointers too.  In a sense, references are just syntactic sugar for pointers.

> When you do "MyClass &myObject", is memory allocated on the free store?

No, declaring a reference does not create an object anywhere.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question