C/C++(C++拷贝构造器,赋值运算符重载)
拷贝构造器
由己存在的对象,创建新对象。也就是说新对象,不由构造器来构造,而是由拷贝构造器来完成。拷贝构造器的格式是固定的。
class 类名
{
类名(const 类名 & another)
拷贝构造体
}
class A
{
A(const A & another)
{}
}
规则:
1 系统提供默认的拷贝构造器。一经实现,不复存在。
2 系统提供的时等位拷贝,也就是所谓的浅浅的拷贝。
3 要实现深拷贝,必须要自定义。
4 浅拷贝,会导致内存重析构。linux下浅拷贝会挂机。double free,在有些情况下(含有堆空间的时候),要实现自拷贝构造
#include <iostream>
#include "mystring.h"
using namespace std;
int main()
{
string s = "assassin";
string ss(s);
cout<<"++++++++++++++++"<<endl;
cout<<ss<<endl;
cout<<"++++++++++++++++"<<endl;
mystring s1 = "intelwisd";
mystring ss1(s1);//浅复制,两个对象指向同一个地址空间,释放对象的时候释放了两次对象所指向的地址空间。
cout<<"++++++++++++++++"<<endl;
cout<<ss1.c_str()<<endl;
cout<<"++++++++++++++++"<<endl;
string sss = s;
mystring sss1 = s1;//也可以实现,本质也是拷贝,用已有一个对象完成一个对象,从无到有的创建过程。
string ssss;
ssss = s;
mystring ssss1;
ssss1 = s1;//默认的也可以,本质是赋值运算符重载---> this指针。
return 0;
}
#ifndef MYSTRING_H
#define MYSTRING_H
class mystring
{
public:
//mystring();
mystring(const char *s = NULL);//无参的形式包含在里面
char * c_str();
mystring(const mystring & another);
~mystring();
private:
char * _str;
};
#endif // MYSTRING_H
#include<iostream>
#include "mystring.h"
#include "string.h"
using namespace std;
mystring::mystring(const char *s)
{
if(s == NULL)
{
_str = new char[1];
*_str = '\0';
}else{
int len = strlen(s);
_str = new char[len+1];
strcpy(_str,s);
}
}
char * mystring::c_str()
{
return _str;
}
mystring::mystring(const mystring & another)
{
//_str = another._str;//同类之间没有隐私,这样的浅复制会造成内存重析构。
int len = strlen(another._str);
_str = new char[len+1];
strcpy(_str,another._str);
}
mystring::~mystring()
{
delete []_str;
}
this 指针
系统在创建对象时,默认生成的指向当前对象的指针。这样作的目的,就是为了带来方便。
作用
1,避免构造器的入参与成员名相同。
2,基于 this 指针的自身引用还被广泛地应用于那些支持多重串联调用的函数中。
class Stu
{
public:
Stu(string name,int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<<name<<"+++"<<age<<endl;
}
Stu & growUp()
{
this->age++;
return *this;
}
private:
string name;
int age;
}
int main()
{
Stu s("assassin",23);
dout<<"&s:"<<&s<<endl;
s.display();
s.growUp().growUp().growUp().growUp().display();//年龄增加
return 0;
}
赋值运算符重载(Operator=)
用一个己有对象,给另外一个己有对象赋值。两个对象均己创建结束后,发生的赋值行为。
格式:
类名
{
类名& operator=(const 类名& 源对象)
拷贝体
}
class A
{
A& operator=(const A& another)
{
//函数体
return *this;
}
};
规则:
1 系统提供默认的赋值运算符重载,一经实现,不复存在。
2 系统提供的也是等位拷贝,也就浅拷贝,会造成内存泄漏,重析构。
3 要实现深深的赋值,必须自定义。
4 自定义面临的问题有三个:1,自赋值
2,内存泄漏
3,重析构。
5 返回引用,且不能用 const 修饰。a = b = c => (a+b) = c
mystring & mystring::operator=(const mystring & another)
{
if(this == &another)//复制自己的情况
return *this;
delete []this->_str;//先把自己的释放掉
int len = strlen(another._str);
this->_str = new char [len+1];
strcpy(this->_str,another._str);
return *this;
}
完整代码:
#include<iostream>
#include "mystring.h"
#include "string.h"
using namespace std;
mystring::mystring(const char *s)
{
if(s == NULL)
{
_str = new char[1];
*_str = '\0';
}else{
int len = strlen(s);
_str = new char[len+1];
strcpy(_str,s);
}
}
char * mystring::c_str()
{
return _str;
}
mystring::mystring(const mystring & another)
{
//_str = another._str;//同类之间没有隐私,这样的浅复制会造成内存重析构。
int len = strlen(another._str);
_str = new char[len+1];
strcpy(_str,another._str);
}
mystring::~mystring()
{
delete []_str;
}
mystring& mystring:: operator=(const mystring & another)
{
if(this == &another)//复制自己的情况
return *this;
delete []this->_str;//先把自己的释放掉
int len = strlen(another._str);
this->_str = new char [len+1];
strcpy(this->_str,another._str);
return *this;
}
#ifndef MYSTRING_H
#define MYSTRING_H
class mystring
{
public:
//mystring();
mystring(const char *s = NULL);//无参的形式包含在里面
char * c_str();
mystring(const mystring & another);
mystring& operator=(const mystring & another);
~mystring();
private:
char * _str;
};
#endif // MYSTRING_H
#include <iostream>
#include "mystring.h"
using namespace std;
int main()
{
string s = "assassin";
string ss(s);
cout<<"++++++++++++++++"<<endl;
cout<<ss<<endl;
cout<<"++++++++++++++++"<<endl;
mystring s1 = "intelwisd";
mystring ss1(s1);//浅复制,两个对象指向同一个地址空间,释放对象的时候释放了两次对象所指向的地址空间。
cout<<"++++++++++++++++"<<endl;
cout<<ss1.c_str()<<endl;
cout<<"++++++++++++++++"<<endl;
string sss;
sss = s;
mystring sss1;//
sss1 = s1;
return 0;
}
C/C++(C++拷贝构造器,赋值运算符重载)的更多相关文章
- C++的转换构造函数、拷贝构造函数、赋值运算符重载
1 转换构造函数 C++的转换构造函数是只有一个参数的构造函数.当程序试图将一个其他类型的对象或基本类型值赋给该类的一个待初始化对象时(如Person p="Dean";) ...
- C++ 一个例子彻底搞清楚拷贝构造函数和赋值运算符重载的区别
class TestChild { public: TestChild() { x=; y=; printf("TestChild: Constructor be called!\n&quo ...
- 一文说尽C++赋值运算符重载函数(operator=)
写在前面: 关于C++的赋值运算符重载函数(operator=),网络以及各种教材上都有很多介绍,但可惜的是,内容大多雷同且不全面.面对这一局面,在下在整合各种资源及融入个人理解的基础上,整理出一篇较 ...
- [转]C++赋值运算符重载函数(operator=)
写在前面: 关于C++的赋值运算符重载函数(operator=),网络以及各种教材上都有很多介绍,但可惜的是,内容大多雷同且不全面.面对这一局面,在下在整合各种资源及融入个人理解的基础上,整理出一篇较 ...
- C++中的赋值运算符重载函数(operator=)
MyStr& operator =(const MyStr& str)//赋值运算符 { cout << "operator =" << e ...
- C++ 中赋值运算符重载以及深拷贝浅拷贝解析
转载自:http://blog.csdn.net/business122/article/details/21242857 关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符 ...
- C++学习之路(五):复制构造函数与赋值运算符重载
之前没有细想过两者的区别,今天对此进行简要记录,后续完善补充. 复制构造函数是在类对象被创建时调用的,但是赋值运算符是被已经存在的对象调用完成赋值操作. 复制构造函数只在对象实例化时才被调用,即在复制 ...
- C++11通过拷贝构造器拷贝const字段(待解决)
问题梗概如标题所述. 在今天实现Token类的时候,遇到的问题. 我希望将Token类设定为immutable属性的,这样实现的方式是将这个类内的所有字段均设置为const型,同时每个字段均为publ ...
- C++ 类的赋值运算符'='重载
什么类需要重载赋值运算符 先来看一个普通类的直接赋值. #include <iostream> using namespace std; class person{ int age; pu ...
随机推荐
- 【codeforces 65A】Harry Potter and Three Spells
[题目链接]:http://codeforces.com/problemset/problem/65/A [题意] 你有3种魔法; 1.可以将a单位的石头变成b单位的铅 2.可以将c单位的铅变成d单位 ...
- POJ 3155 Hard Life
Hard Life Time Limit: 8000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- 使用Openfire和Asmack实现IM功能,常常出现“Thread already started”的错误
近期使用Openfire和Asmack实现Android端的IM功能,可是实际使用的过程中,常常出现"java.lang.IllegalThreadStateException:Thread ...
- RubyMine2017破解
RubyMine2017破解 学习了:https://www.7down.com/soft/172903.html 激活的时候选择 license server; 输入如下地址激活: http://i ...
- Java实现二叉树的创建、递归/非递归遍历
近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...
- [ACM] HDU 1400 Mondriaan's Dream (状态压缩,长2宽1长方形铺满)
Mondriaan's Dream Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- jsoncpp-src-0.5.0.tar.gz 源码错误!!!!
近期在做毕设,使用到了JsonCpp0.5.0版本号的源码! 依照网上的安装配置教程,搭建好环境后就能够使用了! 在这里就不浪费空间去将怎样搭建开发环境了!请大家去google一下就好了! 在解析一个 ...
- nyoj-673-悟空的难题(数组标记)
悟空的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 自从悟空当上了齐天大圣.花果山上的猴子猴孙们便也能够尝到天上的各种仙果神酒,所以猴子猴孙们的体质也得到了非 ...
- bzoj1051: [HAOI2006]受欢迎的牛(强联通)
1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...
- CSS提高渲染速度的写法
写CSS的习惯,决定页面渲染速度的快慢,这一点在脑残的IE里更加明显.养成良好的习惯,乃至形成规范,会让你的页面更快速的加载,用户体验度更高,下面是零度逍遥总结的一些提高CSS渲染速度的写法,供大家参 ...