| | |
Object creation question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Solved Threads: 0
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:
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?
Thanks,
Momar
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:
c++ Syntax (Toggle Plain Text)
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?
Thanks,
Momar
•
•
Join Date: May 2008
Posts: 87
Reputation:
Solved Threads: 8
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.
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.
Optimist: This glass is half full!
Pessimist: This glass is half empty!
Engineering Consultant: This glass is twice as big as it needs to be...
Pessimist: This glass is half empty!
Engineering Consultant: This glass is twice as big as it needs to be...
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:
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
y->Member();
x.Member(); Last edited by CoolGamer48; Jul 29th, 2008 at 4:05 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Solved Threads: 0
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
Momar
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

Momar
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:
In C++, variables of any type, primitive or not, can be directly declared:
One thing you can do is this:
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
Other things we can do with the pointers:
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 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:
C++ Syntax (Toggle Plain Text)
a = new int; b = new myclass;
delete keyword.Other things we can do with the pointers:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
int myInt = 5; a = &myInt; *a = 6;//myInt and *a are both 6 myInt = 7;//myInt and *a are both 7
Last edited by CoolGamer48; Jul 29th, 2008 at 5:08 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Solved Threads: 0
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.
Momar
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.
Momar
![]() |
Similar Threads
- Please help me to convert a simple C++ program into an object-oriented one. (C++)
- Parent/Child Windows References (JavaScript / DHTML / AJAX)
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- Use of constructors (Java)
- virtual methods and inheritance (C++)
- Help with < (less than) operator on classes (C++)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- Object-Oriented Programming (C++)
Other Threads in the C++ Forum
- Previous Thread: memory leak detection tool, need advice
- Next Thread: OS Development
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





