Hello all,

First post here, I'll try to make it understandable heh. I have a question concerning object creation. Let's say I have a class called Node and I want to create an object called myNode. What's the difference between:

Node * myNode = new Node();
Node myNode;

// Then assuming we have some methods
myNode->setValue(1);  //for the first way of creating the object
myNode.setValue(1);    //for the second way listed above

If I compile and trace through, it seems both are calling the necessary constructors, both behave in the same way. What difference exists between the above code?



8 Years
Discussion Span
Last Post by CoolGamer48

Well, if you want to get technical, it has to do with how you are referencing each object at the memory level (and understanding of this depends on how much you know about memory models). Additionally, I think the pointer information is declared on the heap, vs your second declaration which is declared on the stack (someone to confirm this?).

The first is declaring a pointer to the Node struct, and so acts as a pointer. The second is just a straight declaration of the object. You can read up on classes and objects here.

With the first, you have to dereference and then you can access object members (hence the -> operator). With the second, you need only use the "." operator. Otherwise, the first acts like a pointer, the second is simply the object, so use them accordingly.


One very evident difference between the two myNodes is that the second one will be deleted at the end of scope, while the pointer will continue to exist until you manually delete it. Well, that wasn't exactly true. The pointer myNode will be deleted at the end of scope, but pointer myNode doesn't actually hold a node, it holds the address of one. The node at which the pointer myNode points at is what will continue to exist until you delete it.

There are many other differences as well, though whether or not you care about them will depend on your program's intent. For example, myNode can contain the address of another node:

Node x;
Node* y;
y = &x;

//these two calls refer to the same method of the same object
//not two separate objects of the same class

Thanks for the responses.

Ah, so it seems the main difference (other then memory and accessing them) is that one is a pointer and therefore follows all the rules in regards to pointers?

I've learned a bit of Java to start and I seem to recall (but I could be mistaken) that constructors are only called when the keyword "new" is used. But in the C++ code listed above, somehow the constructor is still called. If that was the case, I was wondering why one would be used over the other, other then to have a pointer to the object itself. Doesn't the "Node myNode;" still contain a reference to the object itself? Just that you cannot access it just as you would a pointer?

Thanks for the discussion, it helps me clarify some things in my head heh :)



Don't confuse Java new with C++ new. I don't know Java too well, but new in Java does something else. In Java, variables with primitive types (int, boolean, float, etc.) can be simply declared: int x; boolean b; . Same for C++. However, in Java, when you declare a variable with a non-primitive data type, you aren't actually creating an object of that class, you're creating a reference to it (similar in certain aspects of it's behavior, but not the same as, a pointer). This is the only way to declare objects of classes. The reference must be filled with an actual object using new. Similarly, primitives can't be declared as references (or so I believe). So this: x = new int; will never happen.

In C++, variables of any type, primitive or not, can be directly declared: int x; myclass y; , and you can declare a pointer to any type: int* a; myclass* b; . Now, a and b don't hold objects (nor will they ever). They hold the memory address of objects.

One thing you can do is this:

a = new int;
b = new myclass;

Here, new creates a variable somewhere in memory and returns its address. Now, when you create a variable with C++'s new, you have control of when that object is deleted. In Java (again, I believe), all variables are deleted at the end of scope, regardless of whether they were created by new. In C++, variables created by new continue to exist in memory until you manually delete them with the delete keyword.

Other things we can do with the pointers:

a = &x;
b = &y;

Now, *a and x are the exact same integer, just as *b and y are the exact same object of myclass.

We can overwrite these though, which I believe (deja vu?) you can't in Java:

int myInt = 5;
a = &myInt;
*a = 6;//myInt and *a are both 6
myInt = 7;//myInt and *a are both 7

Ah cool. Yeah in Java you cannot instantiate a primitive type (although there are wrapper classes like Integer and Double that you can use that behave similar). That's good to know, so in C++ you use the new to create the object and return it's address.

Another quick question though, the Node myNode; does this contain a reference (in the same way Java would) to the object?

And yeah, in Java things are deleted when they are out of scope, although you can still have leaks when objects themselves contain references (other objects as members) and things aren't properly cleaned up.

Thanks again.



Another quick question though, the Node myNode; does this contain a reference (in the same way Java would) to the object?

No. myNode is a Node, just like with int x; : x is an int.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.