【拷贝构造函数】
发布网友
发布时间:2022-05-25 06:41
我来回答
共2个回答
热心网友
时间:2024-05-13 05:26
完全正确,给个例子你加深理解。
#include <iostream>
using namespace std;
class point
{
public:
point(int xp=0, int yp=0) : x(xp), y(yp)
{
cout << "This line is from default constructor." << endl;
}
point(const point &rhs)
{
cout << "This line is from copy constructor." << endl;
x = rhs.x;
y = rhs.y;
}
point &operator=(const point &rhs)
{
cout << "Thie line is from operator=."<< endl;
if (this == &rhs)
return *this;
x = rhs.x;
y = rhs.y;
return *this;
}
private:
int x, y;
};
void main()
{
point p;
point p1(p); //A
point p2=p; //B
p2=p; //C
}
热心网友
时间:2024-05-13 05:27
没错,你的理解是正确的!
热心网友
时间:2024-05-13 05:26
完全正确,给个例子你加深理解。
#include <iostream>
using namespace std;
class point
{
public:
point(int xp=0, int yp=0) : x(xp), y(yp)
{
cout << "This line is from default constructor." << endl;
}
point(const point &rhs)
{
cout << "This line is from copy constructor." << endl;
x = rhs.x;
y = rhs.y;
}
point &operator=(const point &rhs)
{
cout << "Thie line is from operator=."<< endl;
if (this == &rhs)
return *this;
x = rhs.x;
y = rhs.y;
return *this;
}
private:
int x, y;
};
void main()
{
point p;
point p1(p); //A
point p2=p; //B
p2=p; //C
}
热心网友
时间:2024-05-13 05:27
没错,你的理解是正确的!