C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

shared pointer for polymorphic types

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
morpheaus
Guest





PostPosted: Fri Jun 24, 2005 7:21 am    Post subject: shared pointer for polymorphic types Reply with 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.

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





PostPosted: Fri Jun 24, 2005 11:24 am    Post subject: Re: shared pointer for polymorphic types Reply with quote



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() );

Quote:
return 0;
}

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





PostPosted: Tue Jun 28, 2005 10:39 am    Post subject: Re: shared pointer for polymorphic types Reply with quote



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





PostPosted: Sun Jul 03, 2005 10:19 am    Post subject: Re: shared pointer for polymorphic types Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.