Solved

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

Posted on 2006-10-27
1
875 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now