论C++类对象赋值
2013-08-19 18:26
106 查看
class Demo
{
public:
Demo()
{}
Demo(int
j)
{
cout<<"Demo int:
"<<j<<endl;
this->i =
j;
}
Demo(const
Demo& d)
{
cout<<"复制构造函数"<<endl;
}
Demo& operator
= (const Demo& d)
{
cout<<"operator
="<<endl;
this->i = d.i;
return *this;
}
int i;
};
int main()
{
Demo
demo1;
Demo
demo2 = demo1;
//当我们使用一个对象来初始化另一个对象时,编译器将自动生成等效语句:Demo demo2 =
Demo(demo1);即复制构造函数
cout<<demo2.i<<endl;
return
0;
}
注意,如果我们把上面程序中的Demo demo2 =
demo1分成两句来写,即:
Demo demo2;
demo2 = demo1;
则会发现调用了调用了 operator =
(demo1),也就是demo2.operator = (demo1);
假设我们想给demo2赋一个其它类型的值呢?比如,整型int等
我们修改程序如下:
class Demo
{
public:
Demo()
{}
Demo(int
j)
{
cout<<"Demo int:
"<<j<<endl;
this->i =
j;
}
Demo(const
Demo& d)
{
cout<<"复制构造函数"<<endl;
}
Demo& operator
= (const Demo& d)
{
cout<<"operator
="<<endl;
this->i = d.i;
return *this;
}
Demo& operator
= (const int x)
{
this->i = x;
cout<<"operator =
int:
"<<x<<endl;
return *this;
}
int i;
};
int main()
{
Demo
demo2 = 3;
//同样,这里等效于Demo demo2 =
Demo(3),也就是带一个整型参数的构造函数,另外说明的是,如果要阻止这种隐式转换,可以在Demo(int
j)构造方法前加上explicit关键字,这时候就只能显式调用Demo(3)才有效了。即写成:Demo demo2 =
Demo(3);
cout<<demo2.i<<endl;
//输出3
return
0;
}
同样,如果我们把Demo demo2 =
3;分成两句来写,即:
Demo demo2;
demo2 = 3;
则会发现调用了调用了 operator =
(3),也就是demo2.operator = (3);
当然,下面给出一种比较复杂点的上述实现:(只为说明,实际很少用)
class Demo
{
public:
Demo()
{}
explicit Demo(int
j)
{
cout<<"Demo int:
"<<j<<endl;
this->i = j;
}
Demo(const
Demo& d)
{
cout<<"复制构造函数"<<endl;
}
Demo&
operator = (const Demo& d)
{
cout<<"operator
="<<endl;
this->i = d.i;
return *this;
}
Demo&
operator = (const int x)
{
//this->i = x;
cout<<"operator =
int:
"<<x<<endl;
return *(new Demo(x));
}
int i;
};
int main()
{
Demo
demo2;
demo2 = 3;
cout<<demo2.i<<endl;
return 0;
}
{
public:
Demo()
{}
Demo(int
j)
{
cout<<"Demo int:
"<<j<<endl;
this->i =
j;
}
Demo(const
Demo& d)
{
cout<<"复制构造函数"<<endl;
}
Demo& operator
= (const Demo& d)
{
cout<<"operator
="<<endl;
this->i = d.i;
return *this;
}
int i;
};
int main()
{
Demo
demo1;
Demo
demo2 = demo1;
//当我们使用一个对象来初始化另一个对象时,编译器将自动生成等效语句:Demo demo2 =
Demo(demo1);即复制构造函数
cout<<demo2.i<<endl;
return
0;
}
注意,如果我们把上面程序中的Demo demo2 =
demo1分成两句来写,即:
Demo demo2;
demo2 = demo1;
则会发现调用了调用了 operator =
(demo1),也就是demo2.operator = (demo1);
假设我们想给demo2赋一个其它类型的值呢?比如,整型int等
我们修改程序如下:
class Demo
{
public:
Demo()
{}
Demo(int
j)
{
cout<<"Demo int:
"<<j<<endl;
this->i =
j;
}
Demo(const
Demo& d)
{
cout<<"复制构造函数"<<endl;
}
Demo& operator
= (const Demo& d)
{
cout<<"operator
="<<endl;
this->i = d.i;
return *this;
}
Demo& operator
= (const int x)
{
this->i = x;
cout<<"operator =
int:
"<<x<<endl;
return *this;
}
int i;
};
int main()
{
Demo
demo2 = 3;
//同样,这里等效于Demo demo2 =
Demo(3),也就是带一个整型参数的构造函数,另外说明的是,如果要阻止这种隐式转换,可以在Demo(int
j)构造方法前加上explicit关键字,这时候就只能显式调用Demo(3)才有效了。即写成:Demo demo2 =
Demo(3);
cout<<demo2.i<<endl;
//输出3
return
0;
}
同样,如果我们把Demo demo2 =
3;分成两句来写,即:
Demo demo2;
demo2 = 3;
则会发现调用了调用了 operator =
(3),也就是demo2.operator = (3);
当然,下面给出一种比较复杂点的上述实现:(只为说明,实际很少用)
class Demo
{
public:
Demo()
{}
explicit Demo(int
j)
{
cout<<"Demo int:
"<<j<<endl;
this->i = j;
}
Demo(const
Demo& d)
{
cout<<"复制构造函数"<<endl;
}
Demo&
operator = (const Demo& d)
{
cout<<"operator
="<<endl;
this->i = d.i;
return *this;
}
Demo&
operator = (const int x)
{
//this->i = x;
cout<<"operator =
int:
"<<x<<endl;
return *(new Demo(x));
}
int i;
};
int main()
{
Demo
demo2;
demo2 = 3;
cout<<demo2.i<<endl;
return 0;
}
相关文章推荐
- C++类对象创建过程(分配空间、赋值和初始化、对象初始化顺序、虚函数表指针)
- C++类对象创建过程(分配空间、赋值和初始化、对象初始化顺序、虚函数表指针)
- C++类对象之间赋值产生内存泄漏的一种情况
- 深入解析C++类对象的赋值复制与类型转换
- C++类对象创建过程(分配空间、赋值和初始化、对象初始化顺序、虚函数表指针)
- JAVA 对象引用,以及对象赋值(转载)
- JAVA 对象引用,以及对象赋值
- 如何限制C++类的对象只能建立在堆上或栈上
- C++类和对象的一个简单的实例
- 每日一题|解压可迭代对象赋值给多个变量
- [深入学习C#]利用反射给对象赋值
- C++类对象大小的计算(三)含有虚函数、虚继承类大小计算
- 同类型对象利用反射自动赋值
- java反射动态给对象属性赋值,并获取属性值
- C++类对象内存模型与成员函数调用分析(中)
- C++类对象的大小
- c++类和对象 (const,内联,友元,静态成员)
- 一个例子说明java数据类型包括对象是赋值还是引用!!!
- springMVC控制器方法POJO入参对象是如何赋值的
- C++类使用构造函数初始化类表和构造函数函数体中赋值的区别