944,005 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2062
  • C++ RSS
Jul 29th, 2008
0

Object creation question

Expand Post »
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:

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Momar is offline Offline
4 posts
since Jul 2008
Jul 29th, 2008
0

Re: Object creation question

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.
Reputation Points: 55
Solved Threads: 10
Junior Poster in Training
n1337 is offline Offline
96 posts
since May 2008
Jul 29th, 2008
0

Re: Object creation question

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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 29th, 2008
0

Re: Object creation question

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Momar is offline Offline
4 posts
since Jul 2008
Jul 29th, 2008
0

Re: Object creation question

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 29th, 2008
0

Re: Object creation question

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Momar is offline Offline
4 posts
since Jul 2008
Jul 29th, 2008
0

Re: Object creation question

Click to Expand / Collapse  Quote originally posted by Momar ...
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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: memory leak detection tool, need advice
Next Thread in C++ Forum Timeline: OS Development





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC