I'm using the CRPT pattern for a complex class (for learning).
I created a complex
base class with a derived cartesian
class, so I can add a complex<polar>
class after. I have this piece of code working properly.
#include <iostream>
template<typename T>
class complex{
public:
using value_type = double;
value_type re(){return static_cast<T*>(this)->re();}
};
class cartesian : public complex<cartesian>{
public:
using value_type = double;
value_type re() const {return 1;}
private:
value_type re_;
};
int main(){
complex<cartesian> c;
std::cout << c.re();
}
It works, but when I want to add a constructor to initialize the re_
variable:
#include <iostream>
template<typename T>
class complex{
public:
using value_type = double;
complex(){static_cast<T*>(this)->cartesian();}
value_type re(){return static_cast<T*>(this)->re();}
};
class cartesian : public complex<cartesian>{
public:
using value_type = double;
cartesian() : re_(2){}
value_type re() const {return re_;}
private:
value_type re_;
};
int main(){
complex<cartesian> c;
std::cout << c.re();
}
I get this error:
main.cpp: In instantiation of ‘complex<T>::complex() [with T = cartesian]’:
main.cpp:15:28: required from here
main.cpp:8:42: error: invalid use of ‘cartesian::cartesian’
8 | complex(){static_cast<T*>(this)->cartesian();}
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
I guess the implementation for constructors is different? Any solution?
Aucun commentaire:
Enregistrer un commentaire