new c 2b 2b

Solutions on MaxInterview for new c 2b 2b by the best coders in the world

showing results for - "new c 2b 2b"
Lupita
03 Apr 2016
1  MyClass * p1 = new MyClass;
2      // allocates memory by calling: operator new (sizeof(MyClass))
3      // and then constructs an object at the newly allocated space
4
5  MyClass * p2 = new (std::nothrow) MyClass;
6      // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
7      // and then constructs an object at the newly allocated space
8
9  new (p2) MyClass;
10      // does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
11      // but constructs an object at p2
12
13  // Notice though that calling this function directly does not construct an 
14  //object:
15  MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
16      // allocates memory by calling: operator new (sizeof(MyClass))
17      // but does not call MyClass's constructor
18
19  delete p1;
20  delete p2;
21  delete p3;