Mike Minaev
Mike Minaev

Reputation: 2062

create reference to template virtual class c++

I have such class

template<typename T>
    class ConnectionStatus:
    {
    public:

        virtual void setStatus(const T& status) = 0;
        virtual T getStatus() = 0;
    };

And i want to have a reference to this class in another class, so i do this: ConnectionStatus<typename T>& status; but compiler said error: template argument 1 is invalid. So how i can make a refernce to template virtual class? Thank you for any help.

Upvotes: 0

Views: 41

Answers (1)

TartanLlama
TartanLlama

Reputation: 65610

There are two main possibilities: when you know what that template argument should be for your class and when you don't.

For the former, it's a simple case of providing it (say it's int in this case):

struct MyClass
{
    ConnectionStatus<int> &m_connection_status;
};

If you don't know the argument, make your class a template class:

template <typename ConnectionStatusType>
struct MyClass
{
    ConnectionStatus<ConnectionStatusType> &m_connection_status;
};

Upvotes: 2

Related Questions