复制构造函数

Copy Constructor

本文关键字:构造函数 复制      更新时间:2023-10-16
class Gun{
private:
    int bullet;
public:
    Gun(int bnum) : bullet(bnum) { }
};
class Police{
private:
    Gun * pistol;
public:
    Police(int bNum) : {
        if(bNum>0)
            pistol = new Gun(bNum);
        else
            pistol=NULL;
    }
    Police(const Police& ref){
        pistol=new Gun(*(ref.pistol)); //Confused about this part.
    }
};

我现在正在学习C++,我对警察的复制构造函数中发生的事情有点迷茫。我相信 Gun 的构造函数只接受整数,但是如何将 *(ref.pistol) 作为参数分配给它?我认为*(ref.pistol)是一个Gun对象,而不是一个整数。

如果您自己没有显式声明复制构造函数,编译器总是隐式声明一个复制构造函数(尽管在某些情况下可以将其删除)。您对此感到困惑的行调用此隐式声明的复制构造函数。

这个隐式声明的复制构造函数是公共的,(如果使用的话)它只是做一个成员复制,即,就好像你已经编写

public:
    Gun(const Gun& other): bullet(other.bullet) {}

我相信 Gun 的构造函数只接受整数,但是如何将 *(ref.pistol) 作为参数分配给它?我认为*(ref.pistol)是一个Gun对象,而不是一个整数。

你是对的,*(ref.pistol)是一把枪,而不是一个整数。C++允许您使用某些特殊方法而不声明它们,编译器将为您生成一些(希望)合适的方法。复制构造函数就是其中之一。

在警察的复制构造函数中,使用默认的 Gun 复制构造函数创建一个新Gun,然后将其分配给this->pistol

如果你想明确地说你想要默认的复制构造函数,你可以在 Gun 中写这个:

Gun(const Gun& other) = default;

这同样适用于构造函数、析构函数、复制/移动构造函数和赋值运算符。

*(ref.pistol)正在访问Gun实例,因此代码正在尝试调用Gun复制构造函数,但您尚未显式定义一个。 您仅定义了非默认非复制构造函数,因此编译器可能会省略创建默认复制构造函数。 不过,您可以显式定义自己的复制构造函数:

class Gun{
private:
    int bullet;
public:
    Gun(int bnum) : bullet(bnum) { }
    Gun(const Gun& ref) : bullet(ref.bullet) { }
};

或者,在 C++11 及更高版本中:

class Gun{
private:
    int bullet;
public:
    Gun(int bnum) : bullet(bnum) { }
    Gun(const Gun& ref) = default;
};

无论哪种方式,您都可以像这样使用它(不要忘记pistol可以是 NULL,因此您必须在 Police 复制构造函数中检查它):

class Police{
private:
    Gun * pistol;
public:
    Police(int bNum) {
        if(bNum>0)
            pistol = new Gun(bNum);
        else
            pistol=NULL;
    }
    Police(const Police& ref) {
        if (ref.pistol)
            pistol=new Gun(*(ref.pistol));
        else
            pistol=NULL;
    }
    ~Police() {
        delete pistol;
    }
};

并且不要忘记将类似的逻辑应用于复制赋值运算符(在处理手动深度复制实现时,不要忘记 3 法则和 C++11 及更高版本中的 5 法则):

class Gun{
private:
    ...
public:
    ...
    // the implicit operator is sufficient in this particular example,
    // this is being shown just for demonstation purposes...
    Gun& operator=(const Gun& ref) {
        bullet = ref.bullet;
        return *this;
    }
    // or, in C++11 and later
    //Gun& operator=(const Gun& ref) = default;
};
class Police{
private:
    ...
public:
    ...
    Police& operator=(const Police& ref) {
        if (ref != *this) {
            delete pistol;
            pistol = NULL;
            if (ref.pistol)
                pistol=new Gun(*(ref.pistol));
        }
        return *this;
    }
    // alternatively:
    /*
    Police& operator=(const Police& ref) {
        if (ref != *this) {
            if (pistol) {
                if (ref.pistol) {
                    *pistol = *(ref.pistol);
                    return *this;
                }
                delete pistol;
                pistol = NULL;
            }
            if (ref.pistol)
                pistol=new Gun(*(ref.pistol));
        }
        return *this;
    }
    */
};