new?
Another newbie question here =-) This one comes from a difference in Java and C++ syntax. In Java, as I understand it, whenever you define an object of a class, you have to use this format:
ClassName object1 = new ClassName(int parameter1);
But in C++ they have two ways to define an object of a class:
className object1(int parameter);
and
className object1 = new className(int parameter);
Why does C++ have 2 ways to do this? What''s the difference between using "new" and not using it?
"I''''m not evil... I just use the power of good in evil ways."
The version using "new" isn''t limited to the scope of the declaration. It stays on the heap until you use "delete" for that variable. If you don''t call ever delete, you get a memory leak (there''s no garbage collection in C/C++, remember; you have to do it yourself).
![](http://libhfs.sourceforge.net/img/thingy.png)
The version calling new also returns a pointer to the object, as opposed to creating a "static" local object. The syntax should be ClassName *object2 = new ClassName(parameter);
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement