C++学习笔记 lesson4 构造函数2
来源:互联网 发布:淘宝工作室是怎么赚钱 编辑:程序博客网 时间:2024/05/16 16:08
#include<iostream> class ClassDemo{public:ClassDemo() {std::cout << "ClassDemo()...被调用" << _num << std::endl;};ClassDemo(int num):_num(num){std::cout << "ClassDemo("<< _num<<")...被调用" << std::endl;};~ClassDemo() {std::cout << "~ClassDemo()...被调用" << _num << std::endl;};ClassDemo & operator=(const ClassDemo&other){std::cout << " operator=(const ClassDemo&)...被调用" << _num << std::endl;_num = other._num;return *this;}ClassDemo(const ClassDemo&other)//拷贝构造函数 参数为引用{std::cout << "ClassDemo(ClassDemo&)...被调用" << _num << std::endl;_num = other._num;}//ClassDemo(const ClassDemo other) 无限递归,编译无法通过//将形参int GetNum() const{return _num;};private:int _num;};void F1(ClassDemo demo) //调用拷贝构造函数产生的临时对象{demo.GetNum(); //,使用的是临时对象//销毁了临时对象 }void F2(const ClassDemo&demo) {demo.GetNum(); //只能调用const方法}int main(){ClassDemo demo = 10;//调用默认构造demo = 20; //==>demo.operator=(ClassDemo(20)) 打印出的_num是调用方的_num//调用默认构造temp//调用默认operator=(const ClassDemo&other) //调用析构ClassDemo demo1 = demo;//ClassDemo(ClassDemo&)被调用ClassDemo demo1(10);// ClassDemo(10)...被调用F1(demo1); //产生了新的临时对象// ClassDemo(&)...被调用//~ClassDemo()...被调用10F2(demo1); //没有新的对象产生return 0;//~ClassDemo()...被调用10} //空类中的方法class ClassDemo{ //默认无参构造// ClassDemo(){} //默认析构// ~ClassDemo(){} //默认拷贝构造// ClassDemo(const ClassDemo& other){} //默认赋值// ClassDemo& operatot=(const CLassDemo other){}//如果有属性private:int _num;//默认拷贝构造// ClassDemo(const ClassDemo& other){num=other._num}//默认赋值// ClassDemo& operatot=(const CLassDemo other){num=other._num}};class MyString{public:MyString(){_len = 100;_str = new char[_len];}MyString(const char*str) :_len(strlen(str)){_str = new char[_len + sizeof(char)];strcpy(_str, str);}~MyString(){if (_str)delete[]_str;}MyString(const MyString & other){delete[]_str;_len = other._len;_str = new char[_len + sizeof(char)];strcpy(_str, other._str); //深拷贝 在对象中,维护了所有参数的生命周期,所有参数的生命周期和我的对象同步 //_str = other._str; //浅拷贝:直接对指针赋值 的行为,存在风险,指针的赋值被delete//_len = other._len; //没有维护参数的生命周期,给别人来维护了}char *GetString(){return _str;}private:char *_str; //有指针的情况int _len;};void test(){MyString str("I love Mark");MyString sb = str;//拷贝构造std::cout << sb.GetString() << std::endl;sb = "I Need Mark!!"; // 产生临时对象,//对sb进行赋值 operator(MyString&)(sb._str=temp._str;_len=temp.len)//析构临时对象temp delete temp._str ==》等同于 delete sb._str(指向同一块区域,已经被释放了,野指针)std::cout << sb.GetString() << std::endl;}
0 0
- C++学习笔记 lesson4 构造函数2
- 【安卓学习笔记】JAVA基础Lesson4-函数重载与构造函数
- Hibernate框架学习笔记 lesson4
- C语言笔记 lesson4 char类型
- (C/C++学习笔记)Copy构造函数应用场景
- 【学习C++】3.构造函数
- 拷贝构造函数学习笔记
- C++构造函数学习笔记
- 构造函数学习笔记(1)
- 学习笔记:构造函数转发
- JS构造函数学习笔记
- OC学习笔记,构造函数
- 学习笔记01-构造函数
- js构造函数学习笔记
- 复制构造函数学习笔记
- c++学习笔记 -- 构造函数
- C++学习笔记--构造函数
- C/C++学习笔记:String类的构造函数、析构函数和赋值函数
- Ubuntu安装tensorflow报错:tensorflow-xx.whl not a supported wheel on this platform
- webSocket简单聊天室(Java_Demo)
- 空串产生式
- 2016总结+2017计划
- 04-树7 二叉搜索树的操作集 (30分)
- C++学习笔记 lesson4 构造函数2
- Android接入微信支付,返回-1
- Ajax 技术详解
- Dubbo架构设计详解
- Cocos2d-x 3.x 图形学渲染系列八
- mysql基本命令
- Fibonacci数列
- 友华光猫超级密码获取——PT622&PT632
- 前端自动化(一)