• copy contructor拷贝构造函数 (Copy Control)


    复制构造函数copy constructor、

    赋值操作符 operator =、

    析构函数destructor:不管类是否定义了自己的析构函数,编译器都自动执行类中非static数据成员的析构函数。

    复制构造函数、赋值操作符、析构函数 总称为复制控制(copy control)。编译器自动实现这些操作,但类也可以定义自己的版本。

    下面代码所使用copy contructor的地方有:

    class Point{
    public:
    	Point(){}
    	Point(const Point & p){
    		static int i=0;
    		cout<<++i<<"call Point copy constructor."<<endl;
    		this->str=p.str;
    	}
    	Point(string s):str(s){}
    	string get_str(){return this->str;}
    private:
    	string str;
    };
    Point global;
    Point foo_bar(Point arg){	//1 use 
    	Point local=arg;	//2 use 
    	Point *heap=new Point(global);	//3 use
    	*heap=local;	//not use
    	Point pa[4]={local,*heap}; //4 use 5 use
    	return *heap;	//6 use
    }
    int main(int argc, char* argv[])
    {
    	Point p("xy");
    	foo_bar(p);
            return 0;
    
    }
    

      

  • 相关阅读:
    CSS+JS实现兼容性很好的无限级下拉菜单
    自动切换的JS菜单
    (2)C#连sqlite
    protobuf编译器
    (67) c# 序列化
    (66) c# async await
    (65)C# 任务
    mbatis 入门
    (64)C# 预处理器指令
    (63)C# 不安全代码unsafe
  • 原文地址:https://www.cnblogs.com/wucg/p/2198466.html
Copyright © 2020-2023  润新知