How to declare copy constructor in derived class, without default construcor in base?
Asked Answered
N

3

19

Please take a look on the following example:

class Base
{
protected:
    int m_nValue;

public:
    Base(int nValue)
        : m_nValue(nValue)
    {
    }

    const char* GetName() { return "Base"; }
    int GetValue() { return m_nValue; }
};

class Derived: public Base
{
public:
    Derived(int nValue)
        : Base(nValue)
    {
    }
    Derived( const Base &d ){
        std::cout << "copy constructor\n";
    }

    const char* GetName() { return "Derived"; }
    int GetValueDoubled() { return m_nValue * 2; }
};

This code keeps throwing me an error that there are no default contructor for base class. When I declare it everything is ok. But when i dont, code does not work.

How can I declare a copy constructor in derived class without declaring default contructor in base class?

Thnaks.

Noctiluca answered 16/2, 2012 at 10:26 Comment(1)
Compiler doesn't supply the default constructor for the class once we have any parameterized constructor..Shower
H
30

Call the copy-constructor (which is generated by the compiler) of the base:

Derived( const Derived &d ) : Base(d)
{            //^^^^^^^ change this to Derived. Your code is using Base
    std::cout << "copy constructor\n";
}

And ideally, you should call the compiler generated copy-constructor of the base. Don't think of calling the other constructor. I think that would be a bad idea.

Hawsepipe answered 16/2, 2012 at 10:29 Comment(2)
@amit: Base(Base const&) will be generated by the compiler!Hawsepipe
@amit: of course it is. unless you define a copy ctor yourself, the compiler will synthesize one for you.Shluh
S
5

You can (and should) call the copy ctor of the base class, like:

Derived( const Derived &d ) :
        Base(d)
{
    std::cout << "copy constructor\n";
}

Note that I turned the Base parameter into a Derived parameter, since only that is called a copy ctor. But maybe you didn't really wanted a copy ctor...

Shluh answered 16/2, 2012 at 10:28 Comment(4)
Base(Base const&) is. By the compiler.Vue
@amit: of course it is. unless you define a copy ctor yourself, the compiler will synthesize one for you.Shluh
@PlasmaHH: You should explicitly mention it in your answer, I totally forgot about it :\Thersathersites
@amit: there is a whole lot of other basic things that are not explicitly mentioned in the answer, because they do not relate to the question, and either I explain all of them, or none. I think none is more useful here.Shluh
K
-1

If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor and a copy constructor as a non-explicit inline public member of its class.

If some user-declared constructors are present, the user may still force the automatic generation of a default constructor by the compiler that would be implicitly-declared otherwise with the keyword default.

In the base constructor, you have user-declared constructors so you must declare otherwise with the keyword default.

class Base
{
protected:
    int m_nValue;

public:
    Base(const Base&) = default;    // since cpp11

    Base(int nValue)
        : m_nValue(nValue)
    {
    }

    const char* GetName() { return "Base"; }
    int GetValue() { return m_nValue; }
};

In the derived constructor, you must construct base constructor like:

Derived(const Base &d)
: Base(d)
{
    std::cout << "copy constructor\n";
}
Kreg answered 17/7, 2020 at 12:16 Comment(1)
This doesn't appear to add anything to the existing eight-year-old answers; it also doesn't correct the copy ctor paramEpigenesis

© 2022 - 2024 — McMap. All rights reserved.