 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
morpheaus Guest
|
Posted: Fri Jun 24, 2005 7:21 am Post subject: shared pointer for polymorphic types |
|
|
I am having a shared_ptr of base class type holding the instance of
derived class. How to get back the stored pointer, and point it to the
derived class.
The commented code shows what is expected .. thanks in advance
class base
{
public:
base(){}
virtual ~base(){}
};
class derived: public base
{
public:
derived(){}
virtual ~derived(){}
};
int main(int argc, char* argv[])
{
boost::shared_ptr<base> pRadio(new derived());
// boost::shared_ptr<derived> pDerived(pRadio);
// derived* ptr = ...
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Fri Jun 24, 2005 11:24 am Post subject: Re: shared pointer for polymorphic types |
|
|
morpheaus wrote:
| Quote: | int main(int argc, char* argv[])
{
boost::shared_ptr<base> pRadio(new derived());
// boost::shared_ptr<derived> pDerived(pRadio);
|
boost::shared_ptr<derived>
pDerived( boost::dynamic_pointer_cast<derived>(pRadio) );
| Quote: | // derived* ptr = ...
|
derived* ptr = dynamic_cast<derived>( pRadio.get() );
I would strongly recommend boost::dynamic_pointer_cast or
boost::static_pointer_cast as they gives you all the lifetime
management goodness of having a shared_ptr to the object rather than a
raw pointer. (Ther
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Tue Jun 28, 2005 10:39 am Post subject: Re: shared pointer for polymorphic types |
|
|
morpheaus wrote:
| Quote: | I am having a shared_ptr of base class type holding the instance of
derived class. How to get back the stored pointer, and point it to the
derived class.
|
Ideally, you don't. You design class hierarchies in such a way that the
required services are accessible through the base class interface.
That's what dynamic polymorphism is about. If you have difficulties
making this work, you should rethink your design and ask yourself
whether the classes involved really fit into a hierarchy.
In reality, things are not always as clear cut. Corner cases abound.
Deadlines loom. The burden of legacy code rests on your shoulders. In
such awkward situations, use the heavy duty tools recommended by Martin
Bonner.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael A Jones Guest
|
Posted: Sun Jul 03, 2005 10:19 am Post subject: Re: shared pointer for polymorphic types |
|
|
Gerhard Menzl wrote:
| Quote: |
I am having a shared_ptr of base class type holding the instance of
derived class. How to get back the stored pointer, and point it to the
derived class.
|
boost defines various pointer_casts for it's set of shared pointers.
template<class T, class U> shared_ptr<T>
shared_polymorphic_downcast(shared_ptr<U> const & r)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|