```c++
#include
#include
class Role
{
public:
int hp;
int mp;
Role(int _hp,int _mp)
{
hp = _hp;
mp = _mp;
};
Role(const Role& r) /*问题在这里*/
{
hp = r.hp;
}
};
int main()
{
Role r(410,200);
Role r2(r);
std::cout << r2.hp << r2.mp << std::endl;
system("pause");
}
为什么这里传递的是const和引用呢,怎么不直接传递一个Role rj进去呢
```
不加const 那么这样代码将不会通过编译
Role func(){
Role result;
return result;
}
Role r1=func();
直接用Role将发生复制
拷贝构造函数,唯一的形参必须是引用,并一定要加const,但是一般会加上const限制。