给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的C++/CLI思辨录之代理构造函数,过去的都会过去,迎接崭新的开始,释放更美好的自己。
【 tulaoshi.com - 编程语言 】
现在我们讨论一下新的C++/CLI环境下的一个很酷的特性,称作代理构造函数。class Foo
{
  private:
   int _mem;
  public:
   Foo() : _mem(0)
   {
    CommonConstructor();
   }
   Foo(int mem) : _mem(mem)
   {
    CommonConstructor();
   }
   Foo(const Foo& f) : _mem(f._mem)
   {
    CommonConstructor();
   }
   // 我们所有的构造器都需要的代码段 
   void CommonConstructor()
   {
    printf("Constructing the object");
   }
};class Foo2
{
  private:
   int _mem;
  public:
   // 该构造器调用第二个称为基类构造器的构造器
   Foo2() : Foo2(0)
   {
   }
   // 下面这个构造器包含由所有构造器使用的公共代码
   Foo2(int mem) : _mem(mem)
   {
    printf("Constructing the object"); 
   }
   Foo2(const Foo2& f) : Foo2(f._mem)
   { 
   }
};来源:http://www.tulaoshi.com/n/20160219/1600541.html
看过《C++/CLI思辨录之代理构造函数》的人还看了以下文章 更多>>