Copy constructor needs to call a method that depends on the object, but constructor can't be virtual

后端 未结 6 619
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 19:24

I have an abstract base class with two inherited classes. In both these classes I define a virtual method that is used by the constructor. Now I need to create a copy constr

相关标签:
6条回答
  • 2021-01-20 20:08

    In a copy constructor of class T you know that the object to be copied is of class T.

    A copy constructor can not construct an object of a derived class.

    It's the derived class' copy constructor that constructs the derived part.

    So you're confused about something: the question does not make sense.

    Presenting an example might clear up what exactly the confusion is about.

    Cheers & hth.,

    0 讨论(0)
  • 2021-01-20 20:17

    Virtual functions called during the constructor/destructor won't resolve to derived type. As stated by Scott Myers here -

    During base class construction, virtual functions never go down into derived classes. Instead, the object behaves as if it were of the base type.

    since you can't use virtual functions to call down from base classes during construction, you can compensate by having derived classes pass necessary construction information up to base class constructors instead.

    0 讨论(0)
  • 2021-01-20 20:20

    There is a nice discussion of the Prototype Pattern here which could be helpful.

    Prototype [Go4]

    Problem

    A "factory" class can't anticipate the type of "product" objects it must create.

    Solution

    Derive all product classes from an abstract Product base class that declares a pure virtual clone() method. The Product base class also functions as the product factory by providing a static factory method called makeProduct(). This function uses a type description parameter to locate a prototype in a static prototype table maintained by the Product base class. The prototype clones itself, and the clone is returned to the caller.

    The reference to 'Go4' is for the seminal book on Design Patterns written by the "Gang of Four" authors.

    0 讨论(0)
  • 2021-01-20 20:22

    Create a virtual method in the object that's passed as an argument to the copy constructor, and call it from the copy constructor. Pass 'this' to that method if necessary.

    0 讨论(0)
  • 2021-01-20 20:25

    We'd have to know exactly what you are trying to achieve to know what the right solution is.

    However there is no problem in calling a virtual function on the object being copied, as that is already a fully constructed object. You can also feed the "this" into it, as long as that function knows that the pointer being passed in is not fully constructed yet. Thus:

    T::T( const T & other )
    {
       other.some_virtual_method(this); // legal but potentially dangerous
    }
    
    void T::some_virtual_method( T* other ) const
    {
       other->another_virtual_method(); // dangerous in this situation
    }
    
    0 讨论(0)
  • 2021-01-20 20:27
    1. Be careful about calling virtual methods in base class constructors.
    2. You may want to use a clone-method.
    0 讨论(0)
提交回复
热议问题