Ray's playground

 

Item 4:Avoid gratuitous default constructors.(More Effective C++)

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class A
 5 {
 6 private:
 7     int a;
 8 public:
 9     A(int n) : a(n){}
10 };
11 
12 int main()
13 {
14     void *rawMemory = operator new[](10* sizeof(A));
15 
16     A *= static_cast<A*>(rawMemory);
17 
18     A* arrayOfA[10];
19     for(int i=0; i<10; i++)
20     {
21         new (&arrayOfA[i]) A(i);
22     }
23 
24     for(int i=0; i<10; i++)
25     {
26         cout << arrayOfA[i] << endl;
27     }
28 
29     cin.get();
30     return 0;
31 }

posted on 2011-06-08 18:03  Ray Z  阅读(312)  评论(0编辑  收藏  举报

导航