简体   繁体   English

复制构造函数,operator= 在子 class

[英]copy constructor, operator= in a child class

I want to make an operator= and copy constructor, to be called in the inherited class.我想创建一个operator=和复制构造函数,以便在继承的 class 中调用。

For normal objects, it works fine, but when I'm trying to call, for example, operator= with a pointer, it is just copying the object address.对于普通对象,它可以正常工作,但是当我尝试使用指针调用operator=时,它只是在复制 object 地址。

So my question is, how can I call those methods with pointers?所以我的问题是,我怎样才能用指针调用这些方法?

#include <iostream>

// base class
class a {     
public:
    //constructors
    a(): x(0), y(1), z(0){ std::cout << "no parameter constructor A\n"; }
    a(int a, int b, int c) :x(a), y(b), z(c){ std::cout << "parameter constructor A\n"; }
    a(const a& ob):x(ob.x), y(ob.y), z(ob.z)
    {
        std::cout << "copy constructor A\n";
    }
    //operator
    a& operator=(const a& obj) 
    {
        if (this != &obj)
        {
            x = obj.x;
            y = obj.y;
            z = obj.z;
        }
        std::cout << "operator = A\n";
        return *this;   
    }
protected:
    int x, y, z;
};

//child class
class b : public a
{
public:
    //constructors
    b() : p(0){ std::cout << "no parameter constructor B\n"; }
    b(int X, int Y, int Z, int B) : a(X, Y, Z), p(B) { std::cout << "parameter constructor B\n"; }
    b(const b& obj) :p(obj.p), a(obj)
    {
        std::cout << "copy constructor B\n";
    }
    //operator =
    b& operator=(const b &obj)
    {
        if (this != &obj)
        {
            p = obj.p;
            &a::operator=(obj);
        }
        std::cout << "operator = B\n";
            return *this;
    }
private:
    int p;
};

int main()
{
    b obj0(4, 8, 16, 32);
    b obj1(obj0);   // copy constructor
    b obj2;
    obj2 = obj1;    // operator =
    std::cout << std::endl << std::endl;
    std::cout << "for pointers:\n\n";
    a* obj3 = new b(4, 8, 16, 32);
    a* obj4(obj3);
    obj4 = obj3;
    return 0;
}

输出

One of the purposes of using pointers (or references) is to avoid needing to create a copy of the object.使用指针(或引用)的目的之一是避免需要创建 object 的副本。 Passing a pointer to the object allows the receiver to refer to and manipulate on the original object.将指针传递给 object 允许接收器参考和操作原始 object。

If you wish the pointer to receive a new object, then you would use new .如果您希望指针接收新的 object,那么您将使用new

When dealing with polymorphism as in your example, you would probably need a virtual method that creates a proper clone (sometimes called a deep copy).在您的示例中处理多态性时,您可能需要一个虚拟方法来创建正确的克隆(有时称为深拷贝)。

class a {
    //...
    virtual a * clone () const = 0;
};

class b : public a {
    //...
    b * clone () const {
        return new b(*this);
    }
};

//...
    a *obj4 = obj3->clone();
//...

We leverage that b * is a covariant return type for a * , so that b::clone() can return a b * , but a::clone() can use the b::clone() as an override and still return an a * .我们利用b *是 a a *的协变返回类型,因此b::clone()可以返回 a b * ,但是a::clone()可以使用b::clone()作为覆盖并且仍然返回a *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM