Überschreiben von typedef in abgeleitetem Template
-
Hallo zusammen,
Ich habe ein Funktion, in der für die aktuelle Klasse ein Objekt erstellt
werden soll. Konkret dachte ich an ein Model, auf dem man ein create_view();
aufrufen kann. Da es ein Template ist, hatte ich eigentlich gehofft,
dass für die abgeleitete Klasse die Funktion auf Basis des typedef darin
neu erstellt wird, leider wird sie es nicht:#include <iostream> class A{}; template<typename T> class FooBar{ public: FooBar(){ std::cout << "normal" << std::endl; } }; template<typename T> class Bar; template<typename T> class FooBar<Bar<T> >{ public: FooBar(){ std::cout << "specialized" << std::endl; } }; template<typename T> class Foo{ public: typedef Foo<T> my_type; FooBar<my_type>* foo(){ return new FooBar<my_type>(); } void foo2(){ FooBar<my_type>(); } }; template<typename T> class Bar : public Foo<T>{ public: typedef Bar<T> my_type; /* FooBar<my_type>* foo(){ return new FooBar<my_type>(); } void foo2(){ FooBar<my_type>(); }*/ }; int main(int argc, char** arg) { Foo<A> foo; foo.foo(); foo.foo2(); Bar<A> bar; bar.foo(); bar.foo2(); return 0; }normal
normal
normal
normalBei der Funktion mit Rückgabe-Typ, kann ich es nachvollziehen, weswegen
er es nicht macht (Unterschiedliche Funktionssignaturen)Ich möchte also quasi das Verhalten als würde ich die Funktionen wie angedeutet
kopieren:normal
normal
specialized
specializedGibt es eine Möglichkeit mein abgestrebtes Verhalten zu verwirklichen
oder muss ich in allen abgeleiteten Klassen die Funktion überschreiben?Gruß,
XSpille
-
Zur Zeit tendiere ich zu einer Lösung ala:
template<typename T> class Helper{ public: typedef T my_type; FooBar<my_type>* foo(){ return new FooBar<my_type>(); } void foo2(){ FooBar<my_type>(); } }; template<typename T> class Foo : public Helper<Foo <T> >{ public: typedef Helper<Foo<T> > type; // ... }; template<typename T> class Bar : public Foo<T>, public Helper<Bar<T> >{ public: typedef Helper<Bar<T> > type; }; int main(int argc, char** arg) { Foo<A> foo; foo.foo(); foo.foo2(); Bar<A> bar; bar.Bar::type::foo(); bar.Bar::type::foo2(); return 0; }EDIT: Eigentlich gefällt mir das gar nicht
