我想学习以下代码在c++/cli中的对应
People* my_people = new People("name","lname");
People* second_people;
&second_people = &my_people;
//
People^ my_people = gcnew People("name","lname");
People^ second_people;
// what is this line?
其实我想把my_people的内容赋值给second_people。 所以当我更改my_people的内容时,second_people的内容必须相同。
最佳答案
首先,您的第一段代码无法编译且毫无意义。 second_people
不指向任何对象,因此您不能将拷贝分配给不存在的对象。相反,你应该写
People* my_people = new People("name","lname");
People* second_people = new People(*my_people);
假设您已经实现了 Rule of Three如你所愿。
现在回到问题。对于 C++/Cli,您还应该像这样实现复制构造函数和赋值运算符
People(const People % other) { ... }
const People % operator = (const People % other) { ... ; return *this;}
如果我们将此应用于您的问题:
People^ my_people = gcnew People("name","lname");
People^ second_people = gcnew People(*my_people);
关于c++ - .net c++/cli 获取指针的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738891/