|
-
December 26th, 2009, 03:42 AM
#1
Declare class pointer with or without new
Hi all,
What are the disadvantages to declaring a C++ class pointer without the new operator? E.g. situation 1:
Situation 2:
Code:
MyClass* myClass = new MyClass();
I understand that using the new operator declares the object on the heap instead of on the stack. However, when I first saw C++ all class pointers were declared without the new keyword. When I do this and compile with g++ with the -Wall option it gives the following warning:
Code:
warning: ‘myClass’ is used uninitialized in this function
Sorry if this is obvious; I've searched around a lot for the advantages and disadvantages and the only thing I can find is the bit about the stack vs. the heap, and that you need to delete any object created on the heap. Is using the stack somehow faster in terms of performance? If both ways are acceptable, why does g++ generate a warning?
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|