Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
model::CRTP< Derived > Class Template Reference

Implementation of the Curiously Recurring Template Pattern (CRTP). CRTP achieves static polyphormism by offerings access to the derived class within the base class. More...

#include <CRTP.h>

Inheritance diagram for model::CRTP< Derived >:
Collaboration diagram for model::CRTP< Derived >:

Public Member Functions

 CRTP ()
 The default constructor.

Protected Member Functions

Derived & derived ()
 A reference to the derived object.
Derived * p_derived ()
 A pointer to the derived object.
const Derived & derived () const
 A reference to the const derived object.
const Derived * p_derived () const
 A pointer to the const derived object.

Detailed Description

template<typename Derived>
class model::CRTP< Derived >

Implementation of the Curiously Recurring Template Pattern (CRTP). CRTP achieves static polyphormism by offerings access to the derived class within the base class.

Definition at line 18 of file CRTP.h.

Constructor & Destructor Documentation

◆ CRTP()

template<typename Derived>
model::CRTP< Derived >::CRTP ( )
inline

The default constructor.

Sample use:

#include <iostream>
#include <CRTP.h>
template <typename Derived>
class BaseClass : public CRTP<Derived>{
public:
void print() const {std::cout<<"Base class of DerivedClass"<< this->p_derived()->id()<<std::endl;}
};
class DerivedClass1 : public BaseClass<DerivedClass1>{
public:
int id() const {return 1;}
};
class DerivedClass2 : public BaseClass<DerivedClass2>{
public:
int id() const {return 2;}
};
int main(){
DerivedClass1 dc1;
DerivedClass2 dc2;
dc1.print();
dc2.print();
}
Derived * p_derived()
A pointer to the derived object.
Definition CRTP.h:27
CRTP()
The default constructor.
Definition CRTP.h:37
int main(int argc, char **argv) int main(argc

Definition at line 37 of file CRTP.h.

Member Function Documentation

◆ derived() [1/2]

template<typename Derived>
Derived & model::CRTP< Derived >::derived ( )
inlineprotected

A reference to the derived object.

Definition at line 24 of file CRTP.h.

◆ derived() [2/2]

template<typename Derived>
const Derived & model::CRTP< Derived >::derived ( ) const
inlineprotected

A reference to the const derived object.

Definition at line 30 of file CRTP.h.

◆ p_derived() [1/2]

template<typename Derived>
Derived * model::CRTP< Derived >::p_derived ( )
inlineprotected

A pointer to the derived object.

Definition at line 27 of file CRTP.h.

◆ p_derived() [2/2]

template<typename Derived>
const Derived * model::CRTP< Derived >::p_derived ( ) const
inlineprotected

A pointer to the const derived object.

Definition at line 33 of file CRTP.h.


The documentation for this class was generated from the following file: