复制构造函数,赋值操作符、关系操作符,const函数
2014-01-12 19:53
393 查看
#include <iostream>
using namespace std;
class A {
private:
int *val;
public:
A(int v) {
cout << "into constructor" << endl;
val = new (int);
*val = v;
}
~A() {
if (val != NULL) {
cout<<"delete pointor val"<<endl;
delete val;
}
}
/**
* 复制构造函数 此处注意 const,用于初始化对象复制而来
*/
A(const A& a){
cout<<"into copy constructor"<<endl;
val = new(int);
*val = *(a.val);
}
/**
* 赋值函数,赋值操作符,同样const ,用于赋值,这个被赋值的对象已经被初始化过
*/
A& operator=(const A& a){
cout<<"into operator ="<<endl;
if(&a != this){
delete val;
val = new(int);
*val = *(a.val);
}
return *this;
}
void setVal(int v) {
if (val != NULL) {
*val = v;
}
}
/**
* const 函数对于外部const 对该对象的引用,可以看到B类里面传入参数为const型,如果这里不加const,B类的那个方法不会编译过通过,代表函数不用修改对象
*/
int getVal() const{
if (val != NULL) {
return *val;
}
return 0;
}
/**
* 关系操作符,类似java中的equals方法
*/
bool operator==(const A& a){
if(*(a.val) == *(val)){
return true;
}
return false;
}
};
class B{
public:
void show(const A& a){//上面方法必须加const
cout<<a.getVal()<<endl;
}
};
int main() {
A a(10);
cout<<a.getVal()<<endl;
A b = a;
b.setVal(13);
cout<<b.getVal()<<endl;
cout<<a.getVal()<<endl;
if(a == b){
cout<<"yes"<<endl;
}
cout<<"===================="<<endl;
a = b;
cout<<a.getVal()<<endl;
if(a == b){
cout<<"true"<<endl;
}
B ba;
ba.show(a);
return 0;
}
运行结果:
=========================================================================
into constructor
10
into copy constructor
13
10
====================
into operator =
13
true
13
delete pointor val
delete pointor val
这篇文章作为我的笔记,自己忘记时就过来看看。
using namespace std;
class A {
private:
int *val;
public:
A(int v) {
cout << "into constructor" << endl;
val = new (int);
*val = v;
}
~A() {
if (val != NULL) {
cout<<"delete pointor val"<<endl;
delete val;
}
}
/**
* 复制构造函数 此处注意 const,用于初始化对象复制而来
*/
A(const A& a){
cout<<"into copy constructor"<<endl;
val = new(int);
*val = *(a.val);
}
/**
* 赋值函数,赋值操作符,同样const ,用于赋值,这个被赋值的对象已经被初始化过
*/
A& operator=(const A& a){
cout<<"into operator ="<<endl;
if(&a != this){
delete val;
val = new(int);
*val = *(a.val);
}
return *this;
}
void setVal(int v) {
if (val != NULL) {
*val = v;
}
}
/**
* const 函数对于外部const 对该对象的引用,可以看到B类里面传入参数为const型,如果这里不加const,B类的那个方法不会编译过通过,代表函数不用修改对象
*/
int getVal() const{
if (val != NULL) {
return *val;
}
return 0;
}
/**
* 关系操作符,类似java中的equals方法
*/
bool operator==(const A& a){
if(*(a.val) == *(val)){
return true;
}
return false;
}
};
class B{
public:
void show(const A& a){//上面方法必须加const
cout<<a.getVal()<<endl;
}
};
int main() {
A a(10);
cout<<a.getVal()<<endl;
A b = a;
b.setVal(13);
cout<<b.getVal()<<endl;
cout<<a.getVal()<<endl;
if(a == b){
cout<<"yes"<<endl;
}
cout<<"===================="<<endl;
a = b;
cout<<a.getVal()<<endl;
if(a == b){
cout<<"true"<<endl;
}
B ba;
ba.show(a);
return 0;
}
运行结果:
=========================================================================
into constructor
10
into copy constructor
13
10
====================
into operator =
13
true
13
delete pointor val
delete pointor val
这篇文章作为我的笔记,自己忘记时就过来看看。
相关文章推荐
- 析构函数、赋值操作符与复制构造函数关系的分析
- 赋值操作符与关系操作符的优先级
- 复制构造函数、赋值操作符与隐式类类型转换
- 重温C++ primer 之复制控制(复制构造函数,赋值操作符,析构函数)
- 继承中,构造函数,赋值操作符,析构函数与虚函数的关系
- 复制构造函数和赋值操作符
- 复制构造函数与赋值操作符之间的区别
- 复制构造函数 赋值操作符
- C++ Primer 学习笔记_16_表达式 --算术、关系、逻辑、位、赋值、自增/自减操作符
- 复制构造函数与赋值操作符的重载
- 【C\C++学习】之十八、C++11六大函数(构造函数,移动构造函数,移动赋值操作符,复制构造函数,赋值操作符,析构函数)
- 复制构造函数、析构函数、赋值操作符缺少其一的后果
- C++学习基础六——复制构造函数和赋值操作符
- 13.1——复制构造函数,赋值操作符,析构函数
- C++11六大函数(构造函数,移动构造函数,移动赋值操作符,复制构造函数,赋值操作符,析构函数)
- 复制构造函数或赋值操作符
- 一个实例介绍C++复制控制(有一个C++11新特性要注意)——复制构造函数、赋值操作符、析构函数
- [C++]复制构造函数、赋值操作符与隐式类类型转换
- Copy Control 复制控制 (复制构造函数 copy constructor,析构函数 destructor, 赋值操作符 operator=
- 复制构造函数和赋值操作符