Solved

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

Posted on 2006-10-27
1
880 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 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