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++语法基础--复制控制--复制构造函数 ,赋值操作符 ,析构函数

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

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

复制构造函数

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

  • 2010-12-03 21:08
  • 187KB
  • 下载

C++构造函数析构函数

  • 2015-06-02 21:17
  • 64KB
  • 下载

c++构造函数和析构函数调用规则

先看如下程序 #include #include #include using namespace std; class Test { public: Test(int a):a(a) ...

C++派生类构造函数和析构函数调用顺序

++派生类构造函数调用顺序(详解) 我们来看下面一段代码:   class B1   {   public:   B1(int i) {cout   };   class B2   {...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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