Solved

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

Posted on 2006-10-27
1
873 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
Comment Utility
> 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

6 Experts available now in Live!

Get 1:1 Help Now