c++中构造函数的相互调用

来源:互联网 发布:fast迅捷网络fwr200 编辑:程序博客网 时间:2024/05/17 07:10

好久没写c++了,最近发现连构造函数的基本知识都忘记了。
还好查到了很多优秀的文章,彻底搞明白了
简短来说,构造函数中一般直接调用其它构造函数是没有用的,要用placement new 才行,具体看下面的参考链接
自己写的一个例子:

#include <iostream>using namespace std;class A{private:    int m;public:    A()    {        new(this)A(2);    }    A(int i):m(i)    {}    void show()    {        cout<<m<<endl;    }};int main(){    A a;    a.show();    return 0;}

参考链接:
http://blog.csdn.net/race604/article/details/6921678
http://www.cnblogs.com/chio/archive/2007/10/20/931043.html
http://blog.csdn.net/zhangxinrun/article/details/5940019
http://www.cnblogs.com/the-tops/p/5587507.html

原创粉丝点击