Reputation: 9527
Is it possible to do this in C++11 in some clean way
class Foo
{
public:
Foo(Bar1 * b1, Bar2 * b2) : bar1(b1), bar2(b2) {}
template <typename T> T * GetData<T>();
private:
Bar1 * bar1;
Bar2 * bar2;
};
template <> Bar1 * Foo::GetData<Bar1>() { return this->bar1;}
template <> Bar2 * Foo::GetData<Bar2>() { return this->bar2;}
And in main code
Foo * foo = new Foo(new Bar1(), new Bar2());
Bar1 * b1 = foo->GetData<Bar1>();
Bar2 * b2 = foo->GetData<Bar2>();
Do it as this, it won't compile.
Upvotes: 0
Views: 48
Reputation: 23813
Remove the <T>
in the template member declaration :
template <typename T> T* GetData();
instead of :
template <typename T> T* GetData<T>();
Upvotes: 3