同类型的“类”可以直接相互赋值么?(C++)

来源:百度知道 编辑:UC知道 时间:2024/06/11 02:33:19

可以!因为C++里有拷贝构造函数,通过拷贝构造函数实现对象相互赋值。
如:

#include <iostream.h>

class student
{
public:
student(){}
student(int n){name = n;}
student(student & stu){} //拷贝构造函数,没写时系统自动分配
void display(){cout<<name<<endl;}
private:
int name;
};

void main()
{
student s1(10),s3;
s3 = s1;
s3.display();
}

最好重载=操作

可以.
你也可以上机试试

不行