c++ - C++中默认复制构造函数的奇怪行为

标签 c++ object constructor copy implicit

我在学习 C++ 中的隐式复制构造函数时遇到了一个奇怪的行为。这是关于这一堆代码:

#include <iostream>

class Person
{
public:
    char* name;
    int age;

    Person( const char* the_name, int the_age )
    {
        name = new char[strlen( the_name ) + 1];
        strcpy( name, the_name );
        age = the_age;
    }

    ~Person()
    {
        delete[] name;
    }
};

int main(){
    Person p1( "Tom", 22 );

    Person p2( p1 );
    p2.name = "Bill";

    // the output is: "Tom"
    std::cout << p1.name << std::endl;

    std::cin.get();
}

默认情况下,复制一个对象意味着复制它的成员,因此在使用默认复制构造函数创建 p2 对象时,它应该只复制一个指针 name,而不是它的字符数组指着。因此,更改对象 p2name 成员应该更改对象 p1name 成员。因此 p1.name 应该是 "Bill" 而不是 "Tom"

我的推理有什么问题?为什么要打印 "Tom"

最佳答案

By default, copying an object means copying its members

是的。

therefore while creating p2 object using default copy constructor it should copy merely a pointer name, not the character array it points to.

正确。

Therefore changing name member of object p2 should change the name member of object p1.

没有。是的,p2.namep1.name指向相同的内存位置,但这并不意味着更改 p1.name 的指针将更改 p2.name 的值.如果那是 int s,如果改变p1.name你会感到惊讶吗?没有影响p2.name ?因为这里也是一样的。您有两个不同的变量,更改其中一个的值不会更改另一个的值。

int a = 0;
int b = 1;

int* ptrA = &a;
int *ptrA2 = &a;

*ptrA = 1; // now a == 1
*ptrA2 = 4; // now a == 4

ptrA2 = &b;

*ptrA2 = 10; // now b == 10
*ptrA = 3; // now a == 3, a != 10  

关于c++ - C++中默认复制构造函数的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131286/

相关文章:

c++ - 超过 1024 个客户端使用 fd_set?

c# - 处理流时 C# 中的 NullReferenceException?

javascript - Lodash 将字符串与对象匹配

java - 构造函数是 OOP 的哪一部分?

java - 为什么我无法访问在构造函数(Java)中创建的类的实例

c# - 如何根据某些参数在构造函数中调用基构造函数?

一个项目中的 C++14 和 C++17

c++ - 我什么时候真的需要使用 atomic<bool> 而不是 bool?

c++ - 使用 CMake 与网络构建 Qt 项目导致 undefined reference

Javascript - 循环对象数组,仅显示最后一个对象