[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppNew
Prefer the use of auto_ptr's (or other smart pointers) over the use of plain pointers [1,2].
new is not supported in C.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppNew
(C++) New
Keyword to dynamically allocate memory and returning a pointer to this memory.Prefer the use of auto_ptr's (or other smart pointers) over the use of plain pointers [1,2].
class MyClass { public: MyClass() { std::cout << "Constructor" << std::endl; ~MyClass() { std::cout << "Destructor" << std::endl; }; int main() { //Bad practice: //MyClass * pMyClass = new MyClass; //delete pMyClass; //Good practice, also shorter. std::auto_ptr<MyClass> pMyClass(new MyClass); return 0; }
- include <iostream>
- include <memory>
new is not supported in C.
Code links
'new' links
References
- Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4
- Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
