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 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?

From Dev

Access to protected members of base class with CRTP

From Dev

Access to protected members of base class with CRTP

From Dev

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

From Dev

How to instantiate inherited class using base constructor?

From Dev

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

From Dev

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

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

Access subclass' property from base class' constructor

From Dev

How to access RTTI in a class constructor?

From Dev

crtp parent access to sister class

From Dev

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

From Dev

C++ CRTP constructor of derived class

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

How to use a copy constructor with a base class?

From Dev

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

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 does a constructor choose a base class constructor in C++

From Dev

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

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

Invalid constructor reference when using local class?

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Access to protected members of base class with CRTP

  4. 4

    Access to protected members of base class with CRTP

  5. 5

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

  6. 6

    How to instantiate inherited class using base constructor?

  7. 7

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

  8. 8

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

  9. 9

    How to make Derived class templated on Base class in CRTP

  10. 10

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

  11. 11

    Access subclass' property from base class' constructor

  12. 12

    How to access RTTI in a class constructor?

  13. 13

    crtp parent access to sister class

  14. 14

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

  15. 15

    C++ CRTP constructor of derived class

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    How to use a copy constructor with a base class?

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

    How to access class properties outside the constructor

  28. 28

    How to access dynamic array inside a class constructor?

  29. 29

    Invalid constructor reference when using local class?

HotTag

Archive