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

原创 2015年11月18日 00:16:29
#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











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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++语法基础--复制控制--复制构造函数 ,赋值操作符 ,析构函数

复制控制(编译器自动实现这些操作)             *复制构造函数             *赋值操作符              *析构函数          1.复...

一个实例介绍C++复制控制(有一个C++11新特性要注意)——复制构造函数、赋值操作符、析构函数

复制构造函数

C++构造函数与析构函数

  • 2010年12月03日 21:08
  • 187KB
  • 下载

C++ 派生类构造函数、析构函数调用 [大三TJB_708]

《C++面向对象程序设计》第二版 杜茂康 李昌兵   1 派生类构造函数、析构函数描述 作用和形式 派生类可能有多个基类,也可能包括多个对象成员(指本类中的数据成员,此数据成员为某类定义的对象)。在创...

C++构造函数析构函数

  • 2015年06月02日 21:17
  • 64KB
  • 下载

Java程序员学习C++之构造函数和析构函数调用的时机

今天看书忽然对这个地方有点模糊,尤其是析构函数在调用默认的析构函数和用户自己覆写的析构函数的时候有点意识模糊呢。写段代码总结下   #include       using n...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c++ 默认构造函数,构造函数,复制构造函数,赋值操作符,析构函数调用示例
举报原因:
原因补充:

(最多只允许输入30个字)