Neel Basu
Neel Basu

Reputation: 12904

sub classing from enable_shared_from_this

I've a generic_connection

class generic_connection: public boost::enable_shared_from_this<generic_connection>

Now I want to subclass it and create

class agent_connection: public generic_connection

does agent_connection need to derive from boost::enable_shared_from_this<agent_connection> again ?

Upvotes: 2

Views: 133

Answers (2)

Galimov Albert
Galimov Albert

Reputation: 7357

You dont need to derieve again. But, this has some problems, for example you cannot do call like this

shared_from_this()->agent_connection__method()

or this

boost::bind(&agent_connection::method, shared_from_this())

To solve this, you should do templated inheritance:

template <typename T>
class generic_connection : 
        public boost::enable_shared_from_this<T> {
};

class agent_connection : public generic_connection< agent_connection > {
};

This makes agent_connection more complicated, but you wont need to cast shared_ptr any time you use it.

Upvotes: 2

user1804599
user1804599

Reputation:

No.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Upvotes: 5

Related Questions