C++ Object Declaration - What's the difference between Object* and Object& and Object.
Posted on 2006-10-27
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? */
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?