How to access base class constructor when using CRTP

Honza

I need to insert clone and create member functions to my class hieararchy

class Base
{
protected:
    const int x_;
public:
    Base() : x_(0) {}
    Base(int x) : x_(x) {}
};

I thought that CRTP could be the way how to save some typing and avoid errors.

template <typename Derived>
class CRTP_Iface : public Base
{
public:
    virtual Base *create() const { return new Derived(); }
    virtual Base *clone() const { return new Derived(static_cast<Derived const&>(*this)); }
};

Unfortunately I'm not able to access base class constructor to initialize const members.

class D1 : public CRTP_Iface<D1>
{
public:
    D1() : Base() {}
    D1(int x) : Base(x) {}
};

class D2 : public CRTP_Iface<D2>
{
public:
    D2() : x_(0) {}
    D2(int x) : x_(x) {}
};

int main()
{
    D1 a;
    D2 b;

    return 0;
}

Is there any simple way how to solve this?

Kornel Kisielewicz

Simply add all needed constructors to CRTP_Iface.

public:
  CRTP_Iface() : Base() {}
  CRTP_Iface( int x ) : Base(x) {}

If using C++11 this gets even easier:

public:
  using Base::Base;

Then you have:

class D1 : public CRTP_Iface<D1>
{
public:
    D1() : CRTP_Iface() {}
    D1(int x) : CRTP_Iface(x) {}
};

... which can be nicer written in C++11:

class D1 : public CRTP_Iface<D1>
{
public:
  using CRTP_Iface<D1>::CRTP_Iface;
};

(not sure if is needed on either left hand or right hand of ::, AFAIR some complilers like it more strict)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to inherit base class properties and call base class constructor in javascript?

From Dev

Can a derived class access constructor and destructor of base class?

From Dev

How to make Derived class templated on Base class in CRTP

From Dev

Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

From Dev

How to access RTTI in a class constructor?

From Dev

Access to protected members of base class with CRTP

From Dev

C++ CRTP constructor of derived class

From Dev

Using typedefs of a non-template derived class in the base class when using CRTP

From Dev

Invalid constructor reference when using local class?

From Dev

How to use base constructor data to another constructor in the same class?

From Dev

When is a Java Implicit Constructor called compared to the Base Class Constructor?

From Dev

How to instantiate inherited class using base constructor?

From Dev

How does a constructor choose a base class constructor in C++

From Dev

CRTP (Curiously Recurring Template Pattern) using a generic base template class instead of the derived class

From Dev

Access subclass' property from base class' constructor

From Dev

CRTP and visibility of a type that is defined by the base class

From Dev

How to do inhereted empty constructor when Base class has constructor with parameters

From Dev

crtp parent access to sister class

From Dev

How to break at a constructor of base class using gdb?

From Dev

How to use a copy constructor with a base class?

From Dev

when using "new" to allocate memory to a Derived class, how to call the Base constructor?

From Dev

Access to protected members of base class with CRTP

From Dev

How to use base constructor data to another constructor in the same class?

From Dev

How to access class properties outside the constructor

From Dev

How to access dynamic array inside a class constructor?

From Dev

CRTP and visibility of a type that is defined by the base class

From Dev

Why do I need to access the base class constructor with super()?

From Dev

How can I call the constructor of the derived class when using CRTP?

From Dev

How to make an optional template parameter with a base class using CRTP?

Related Related

  1. 1

    How to inherit base class properties and call base class constructor in javascript?

  2. 2

    Can a derived class access constructor and destructor of base class?

  3. 3

    How to make Derived class templated on Base class in CRTP

  4. 4

    Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

  5. 5

    How to access RTTI in a class constructor?

  6. 6

    Access to protected members of base class with CRTP

  7. 7

    C++ CRTP constructor of derived class

  8. 8

    Using typedefs of a non-template derived class in the base class when using CRTP

  9. 9

    Invalid constructor reference when using local class?

  10. 10

    How to use base constructor data to another constructor in the same class?

  11. 11

    When is a Java Implicit Constructor called compared to the Base Class Constructor?

  12. 12

    How to instantiate inherited class using base constructor?

  13. 13

    How does a constructor choose a base class constructor in C++

  14. 14

    CRTP (Curiously Recurring Template Pattern) using a generic base template class instead of the derived class

  15. 15

    Access subclass' property from base class' constructor

  16. 16

    CRTP and visibility of a type that is defined by the base class

  17. 17

    How to do inhereted empty constructor when Base class has constructor with parameters

  18. 18

    crtp parent access to sister class

  19. 19

    How to break at a constructor of base class using gdb?

  20. 20

    How to use a copy constructor with a base class?

  21. 21

    when using "new" to allocate memory to a Derived class, how to call the Base constructor?

  22. 22

    Access to protected members of base class with CRTP

  23. 23

    How to use base constructor data to another constructor in the same class?

  24. 24

    How to access class properties outside the constructor

  25. 25

    How to access dynamic array inside a class constructor?

  26. 26

    CRTP and visibility of a type that is defined by the base class

  27. 27

    Why do I need to access the base class constructor with super()?

  28. 28

    How can I call the constructor of the derived class when using CRTP?

  29. 29

    How to make an optional template parameter with a base class using CRTP?

HotTag

Archive