构造函数,拷贝构造函数,析构函数
来源:互联网 发布:程序员数学 编辑:程序博客网 时间:2024/04/30 20:56
#include<iostream.h>class Point{public: Point() //不显示的写出来就会被屏蔽? { } Point(int a, int b) //一个函数定义在类的里面就是inline吗? { //this.x = a; 错误:this是指针,指向当前正在被操作的对象 //this.y = b; this->x = a; //这样就体现了构造函数的作用,将变量与外界相互隔离 this->y = b; cout<<"创建的构造函数"<<endl; } Point(int a, int b, int c) //构造函数重载:函数名相同,参数个数或类型不同 { //this.x = a; 错误:this是指针,指向当前正在被操作的对象 //this.y = b; this->x = a; //这样就体现了构造函数的作用,将变量与外界相互隔离 this->y = b; int k=c; cout<<"重载的构造函数"<<endl; }// Point(int a, int b):x(a),y(b) //构造函数的初始化表,效率更高 // {// cout<<"创建的构造函数"<<endl;// } Point(Point &p)//拷贝构造函数的参数是类的引用 { x = p.x; //注意不要写成p.x = x;因为我们操作的是当前的对象,p是已经存在的对象的引//用,复制的源对象 y = p.y; cout<<"调用拷贝构造函数"<<endl; } void show() { cout<<"x="<<this->x<<endl; cout<<"y="<<this->y<<endl; } /*析构函数:不能显示的被调用,没有参数,所以不能被重载. 析构函数主要作用就是按照用户的要求进行清理工作,如 在前面用malloc/new申请了内存,则应该在析构函数中调用 free/delete返还相应的,以及其他用户希望在最后一次使用 对象之后所执行的任何操作*/ ~Point() { cout<<"创建的析构函数"<<endl; }private: int x; int y;};void funTest1(Point p){ p.show();}Point funTest2(){ Point A(1, 2); //Point A; 不对,在我们写了自己的构造函数(都有参数)后默认构造函数会被自动屏蔽? return A;}int main(){ Point m(2, 3); //m.show(); //拷贝构造函数在三种情况下会调用: //1.对象的定义 //Point n(m); //调用 //2.动态创建对象 //Point* p = new Point(m); //3.函数的传值调用 //funTest1(m); //4.函数的返回值是类的对象 //Point n; //n=funTest2(); 不对 //构造 //Point a(1, 2); //Point b = a; //构造,会调用拷贝构造函数 (复制) //Point a(1, 2); //Point b; //b = a; //赋值, 不会调用拷贝构造函数 return 0;}
对于拷贝狗杂函数又涉及到深度拷贝和浅拷贝
参考:http://www.afanihao.cn/videos/cppguide/content.htm
#include <stdio.h>#include <stdlib.h>#include <string.h>class Text{public: Text(const char* str) { // 申请一块内存, 保存此字符串 m_size = strlen(str) + 1; m_buf = new char[m_size]; strcpy(m_buf, str); } //深度拷贝 我们自己写的拷贝构造函数就是要解决深度拷贝的问题,进行浅拷贝是 //多余的,因为默认的拷贝构造函数就能做这些工作了 //Text(const Text& other) //{ // m_size = other.m_size; // m_buf = new char[m_size]; // strcpy(m_buf, other.m_buf); //} //浅拷贝,就是对成员指针变量也使用"="进行赋值, 也就是默认 //的拷贝构造函数干的事,意思是不写这个函数也会有一个默认的这样的函数并且事项了此函数的功能 Text(const Text& other) { m_size = other.m_size; m_buf = other.m_buf; //就是把我们现在正在操作的对象中的指针变量指向 //传过来的对象的相应的指针变量 } //总结:只有在需要进行深度拷贝时,才自己添加拷贝构造函数 ~Text() { // 释放此字符串 delete [] m_buf; }private: int m_size; char* m_buf;};int main(){ Text t1("helloworld"); Text t2(t1); return 0;}
对于上面的代码,当使用深度拷贝的拷贝构造函数时可以看到:
m_buf指向不同的地址。
当使用浅拷贝的拷贝构造函数时可以看到:
m_buf指向相同的地址,其实只是同一段内存.
0 0
- 构造函数,拷贝构造函数,赋值函数,析构函数
- 构造函数,拷贝构造函数,赋值函数,析构函数
- 构造函数、析构函数、拷贝构造函数,常函数
- 构造函数、析构函数、拷贝构造函数、赋值函数
- 构造函数--拷贝构造函数--析构函数
- C++学习体会--构造函数,析构函数,拷贝构造函数
- 构造函数、析构函数、拷贝构造函数
- C++构造函数析构函数拷贝构造函数
- 构造函数,拷贝构造函数和析构函数
- C++中构造函数,拷贝构造函数,析构函数
- 构造函数,拷贝构造函数,析构函数
- 构造函数、析构函数、拷贝构造函数小结
- 构造函数、拷贝构造函数和析构函数
- c++ 构造函数,拷贝构造函数,析构函数小结
- c++ 构造函数 拷贝构造函数 析构函数 总结
- C++中的构造函数+拷贝构造函数+析构函数
- 构造函数,拷贝构造函数,析构函数
- C++ 构造函数,析构函数,拷贝构造函数
- uva11762(期望)
- Redis学习笔记---字典类型
- error: jump to case label
- 搭建Spring MVC环境进行简单的增、删、改、查(二)
- linux常用目录作用
- 构造函数,拷贝构造函数,析构函数
- .Net Sql Server use informix_odbc tips
- redis的PHP封装
- android 5.0+原生质设计,cardview、Recyclerview、百分比布局PercentRelativeLayout简单使用
- adb命令 logcat查看日志
- HDU 5326 公司人员管理树问题(多校)-简单dp
- 浅谈MFC单文档(SDI)程序
- iOS 之选择器(Selector)
- neusoft.shixi__任务一