Object creation question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 4
Reputation: Momar is an unknown quantity at this point 
Solved Threads: 0
Momar Momar is offline Offline
Newbie Poster

Object creation question

 
0
  #1
Jul 29th, 2008
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:

  1. Node * myNode = new Node();
  2. Node myNode;
  3.  
  4. // Then assuming we have some methods
  5. myNode->setValue(1); //for the first way of creating the object
  6. 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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 87
Reputation: n1337 is on a distinguished road 
Solved Threads: 8
n1337 n1337 is offline Offline
Junior Poster in Training

Re: Object creation question

 
0
  #2
Jul 29th, 2008
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.
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...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Object creation question

 
0
  #3
Jul 29th, 2008
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
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...".
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 4
Reputation: Momar is an unknown quantity at this point 
Solved Threads: 0
Momar Momar is offline Offline
Newbie Poster

Re: Object creation question

 
0
  #4
Jul 29th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Object creation question

 
0
  #5
Jul 29th, 2008
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:
  1. a = new int;
  2. 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:
  1. a = &x;
  2. 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:
  1. int myInt = 5;
  2. a = &myInt;
  3. *a = 6;//myInt and *a are both 6
  4. 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...".
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 4
Reputation: Momar is an unknown quantity at this point 
Solved Threads: 0
Momar Momar is offline Offline
Newbie Poster

Re: Object creation question

 
0
  #6
Jul 29th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Object creation question

 
0
  #7
Jul 29th, 2008
Originally Posted by Momar View Post
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC