Solved

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

Posted on 2006-10-27
1
883 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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