new?

Started by
2 comments, last by Tac-Tics 22 years, 6 months ago
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."
Advertisement
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).

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!
Much thanks

"I''''m not evil... I just use the power of good in evil ways."

This topic is closed to new replies.

Advertisement