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

ruipacheco

I have the following setup:

#include <iostream>
template <typename T>
struct feline {
  void roar() noexcept {
      static_cast<T*>(this)->do_roar();
  }
      feline() noexcept {
      std::cerr << "Feline ctor" << std::endl;
  }
};

struct lion : public feline<lion> {
  lion() noexcept : feline() {
    std::cerr << "Lion ctor" << std::endl;
  }
  void do_roar() noexcept {
      std::cerr << "Lion roar" << std::endl;
  }
};

struct tiger : public feline<tiger> {
  tiger() noexcept : feline() {
    std::cerr << "Tiger ctor" << std::endl;
  }
  void do_roar() noexcept {
      std::cerr << "Tiger roar" << std::endl;
  }
};

int main()
{
    feline<lion> lion;
    lion.roar();
    feline<tiger> tiger;
    tiger.roar();
}

and when I execute it I get the following result:

Feline ctor
Lion roar
Feline ctor
Tiger roar

Which means the constructors for Lion and Tiger are never called. How can I make that happen?

JuanR

Because you never create an actual lion.

CRTP requires an object of the actual class in order to work properly.

Remember that the base class will never instantiate the child class directly because its all about forcing the class to support some functions and not classical inheritance.

Your code could crash but it doesn't, and that's because the function do_roar does not access any private variables. Try adding some members to lion and using them in do_roar, you'll see...

If you want a lion use the lion class, feline<lion> is just an instantiation of feline<T> where T = lion.

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 combine templated derived class in CRTP with derived class expression templates?

From Dev

How to access base class constructor when using CRTP

From Dev

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

From Dev

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

From Dev

How can I do constructor overloading in a derived class in TypeScript?

From Dev

Can I forward template arguments of a derived class to the base in CRTP?

From Dev

C++ CRTP constructor of derived class

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

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

From Dev

Base Constructor Call in Derived Class

From Dev

How is dynamic polymorphism useful when I cant call derived class methods with base class reference

From Dev

How is dynamic polymorphism useful when I cant call derived class methods with base class reference

From Dev

How do I access an inner class constructor from a derived class?

From Dev

Can I call the constructor and destructor of a class explicity?

From Dev

Call derived class method when using base class in foreach loop

From Dev

How to make Derived class templated on Base class in CRTP

From Dev

How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

From Dev

Can I call a derived method from base class?

From Dev

Can I call a constructor from the constructor of another class in C#?

From Dev

How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

From Dev

How can a derived class pointer to a base class object call methods of the derived class?

From Dev

How can a derived class pointer to a base class object call methods of the derived class?

From Dev

Error when when creating Derived Class constructor

From Dev

Can I use CRTP with multiple derived classes, and use them polymorphically?

From Dev

Can I pass arguments to a base constructor from a derived class's default constructor?

From Dev

Can I pass arguments to a base constructor from a derived class's default constructor?

Related Related

  1. 1

    How can I combine templated derived class in CRTP with derived class expression templates?

  2. 2

    How to access base class constructor when using CRTP

  3. 3

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

  4. 4

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

  5. 5

    How can I do constructor overloading in a derived class in TypeScript?

  6. 6

    Can I forward template arguments of a derived class to the base in CRTP?

  7. 7

    C++ CRTP constructor of derived class

  8. 8

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  9. 9

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  10. 10

    How do I call a derived class method from the base class constructor in C++?

  11. 11

    How to call constructor of a template base class in a template derived class?

  12. 12

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

  13. 13

    Base Constructor Call in Derived Class

  14. 14

    How is dynamic polymorphism useful when I cant call derived class methods with base class reference

  15. 15

    How is dynamic polymorphism useful when I cant call derived class methods with base class reference

  16. 16

    How do I access an inner class constructor from a derived class?

  17. 17

    Can I call the constructor and destructor of a class explicity?

  18. 18

    Call derived class method when using base class in foreach loop

  19. 19

    How to make Derived class templated on Base class in CRTP

  20. 20

    How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

  21. 21

    Can I call a derived method from base class?

  22. 22

    Can I call a constructor from the constructor of another class in C#?

  23. 23

    How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

  24. 24

    How can a derived class pointer to a base class object call methods of the derived class?

  25. 25

    How can a derived class pointer to a base class object call methods of the derived class?

  26. 26

    Error when when creating Derived Class constructor

  27. 27

    Can I use CRTP with multiple derived classes, and use them polymorphically?

  28. 28

    Can I pass arguments to a base constructor from a derived class's default constructor?

  29. 29

    Can I pass arguments to a base constructor from a derived class's default constructor?

HotTag

Archive