I want to put any kind of Object (Object, Object etc.) into one shared_ptr. So I created base class and use shared_ptr.
But, how can I declare
T getMember();
within the base class so I can call ObjectBase.getMember?
class ObjectBase
{
public:
//virtual getMember HOWTO?
};
template<typename T>
class Object : public ObjectBase
{
public:
Object(T x):member(x) { }
T getMember() { return member; }
private:
T member;
};
You can’t. How should such a declaration look, that it can return all kinds of types? It’s just not possible. You’d have to cast the
ObjectBase*to the correctObject<T>*and then use thegetMemberfunction. It’s only possible if allTshare a common base class, so you could return a pointer to that. But that would put a strict constraint onT.