关闭

c++ 默认构造函数,构造函数,复制构造函数,赋值操作符,析构函数调用示例

123人阅读 评论(0) 收藏 举报
分类:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <fstream>
#include <list>
#include <string>



struct Noname
{
        //default constructor
        Noname():pstring(new std::string),i(0),d(0) {  std::cout<<"default constructor called"<<std::endl;}
        // constructor
        Noname(int in,double dou):pstring(new std::string),i(in),d(dou) { std::cout<<"constructor called"<<std::endl;}
        //copy constructor
        Noname (const Noname & no):pstring(new std::string),i(no.i),d(no.d)
        {
            std::cout<<"copy constructor called"<<std::endl;
            *pstring = * (no.pstring);
        }

        //assign operator
        Noname& operator=(const Noname &);

        ~Noname()
        {
            std::cout<<"destructor is called "<<std::endl;
            delete pstring;

        }

    public:
        std::string *pstring;
        int i;
        double d;
};



Noname& Noname::operator=(const Noname & no)
{
    std::cout<<"assign operator called "<<std::endl;
    *pstring = * (no.pstring);
    i = no.i;
    d = no.d;
}

void fuck_test1(Noname no)
{


}




void fuck_test2(Noname & no)
{


}




Noname fuck_test3()
{
    Noname no;
    return no;
}


Noname&  fuck_test4()
{
    Noname no;
    return no;
}
</pre><pre name="code" class="cpp">
int main(int argc,char* argv[])
{
    Noname test(5,3);
    std::cout<<test.i<<"   "<<test.d<<std::endl;
    Noname test1(test);
    std::cout<<test1.i<<"   "<<test1.d<<std::endl;
    Noname test2;
    std::cout<<test2.i<<"   "<<test2.d<<std::endl;
    test2 =  test1;
    std::cout<<test2.i<<"   "<<test2.d<<std::endl;

    if(true)
    {
        Noname testk(5,5);

    }
</pre><pre name="code" class="cpp">     Noname* pNoname = new Noname();
     delete pNoname;


    std::vector<Noname> Novect(5);


    fuck_test1(test);
    fuck_test2(test);
    fuck_test3();
    fuck_test4();

    return 0;
}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">
</pre><p></p><pre name="code" class="cpp">



程序运行结果:

constructor called
5   3
copy constructor called
5   3
default constructor called
0   0
assign operator called 
5   3
constructor called
destructor is called
default constructor called
destructor is called
default constructor called
default constructor called
default constructor called
default constructor called
default constructor called
copy constructor called
destructor is called
default constructor called
destructor is called
default constructor called
destructor is called
destructor is called
destructor is called
destructor is called
destructor is called
destructor is called
destructor is called
destructor is called
destructor is called











仔细的研究下本程序,会对构造函数,析构函数,复制构造函数,赋值操作符的概念容会贯通;

同时会对容器元素的构造,动态对象的创建,销毁, 非引用参数形参传递和引用形参传,  非引用返回值,引用返回值的原理有更直观的理解

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:39750次
    • 积分:1672
    • 等级:
    • 排名:千里之外
    • 原创:126篇
    • 转载:18篇
    • 译文:0篇
    • 评论:5条
    最新评论