polymorphism and boost::shared_ptr
Greetings, This is my first post to the boost users list and I am a relative new comer to this wonderful template library, so have some mercy on me. I am sure this question has been asked before and I tried to find it via google without any luck. If someone could show me how to make the following polymorphism work so that AddChild of class A will add type B and type C class I would really appreciate it. I don't want to have to typecast in this situation, but I can't see any other way. Please forgive me if this is a basic question, but I am new to some of these concepts. Kind regards Brad typedef boost::shared_ptr<A> shPtrA; typedef boost::shared_ptr<B> shPtrB; typedef boost::shared_ptr<C> shPtrC; class A { void AddChild(shPtrA& child); private: std::vector<shPtrA> m_children; } class B : public A { } class C: public B { } main() { A base; shPtrB spB(new B()); shPtrC spC(new C()); // I dont want to typecast spB or spC A.AddChild( (shPtrA) spB); A.AddChild( (shPtrA) spC); }
On 9/7/07, Brad Ryder
main() { A base; shPtrB spB(new B()); shPtrC spC(new C());
// I dont want to typecast spB or spC
A.AddChild( (shPtrA) spB); A.AddChild( (shPtrA) spC); }
Simply stop using shPtrB and shPtrC, and just use shPtrA. e.g., main() { A base; shPtrA spB(new B()); shPtrA spC(new C()); // works fine A.AddChild(spB); A.AddChild(spC); } HTH, --Michael Fawcett
Hi Michael,
Thanks for the response. Your solution will indeed work, but the side
effect is that spB and spC only have access to methods available in
the base class. Imagine that B & C have unique methods that I want to
access in main like.
main()
{
A base;
shPtrB spB(new B());
shPtrC spC(new C());
spB.PrintBrad(); // unique to B
spC.PrintMichael(); // unique to C
// this will fail obviously, but how else can I get access to unique
methods in B & C
base.AddChild(spB);
base.AddChild(spC);
}
Thanks for your help
Brad
On 9/7/07, Michael Fawcett
On 9/7/07, Brad Ryder
wrote: main() { A base; shPtrB spB(new B()); shPtrC spC(new C());
// I dont want to typecast spB or spC
A.AddChild( (shPtrA) spB); A.AddChild( (shPtrA) spC); }
Simply stop using shPtrB and shPtrC, and just use shPtrA. e.g.,
main() { A base; shPtrA spB(new B()); shPtrA spC(new C());
// works fine A.AddChild(spB); A.AddChild(spC); }
HTH,
--Michael Fawcett _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 9/7/07, Brad Ryder
Hi Michael,
Thanks for the response. Your solution will indeed work, but the side effect is that spB and spC only have access to methods available in the base class. Imagine that B & C have unique methods that I want to access in main like.
main() {
A base; shPtrB spB(new B()); shPtrC spC(new C());
spB.PrintBrad(); // unique to B spC.PrintMichael(); // unique to C
// this will fail obviously, but how else can I get access to unique methods in B & C base.AddChild(spB); base.AddChild(spC); }
Thanks for your help
Try to remember not to top-post. Generally you should not need to know if the instances are indeed A, B, or C. If you did, why would you be storing them in a container of base class pointers? If you find you really need to perform the cast, look at boost::static_pointer_cast. --Michael Fawcett
On 9/7/07, Michael Fawcett
On 9/7/07, Brad Ryder
wrote: Hi Michael,
Thanks for the response. Your solution will indeed work, but the side effect is that spB and spC only have access to methods available in the base class. Imagine that B & C have unique methods that I want to access in main like.
main() {
A base; shPtrB spB(new B()); shPtrC spC(new C());
spB.PrintBrad(); // unique to B spC.PrintMichael(); // unique to C
// this will fail obviously, but how else can I get access to unique methods in B & C base.AddChild(spB); base.AddChild(spC); }
Thanks for your help
Try to remember not to top-post.
Generally you should not need to know if the instances are indeed A, B, or C. If you did, why would you be storing them in a container of base class pointers?
If you find you really need to perform the cast, look at boost::static_pointer_cast.
--Michael Fawcett _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Micheal, Sorry about the top post and thank you again for the response. I guess I really need to know how and if it's possible to make the base class A method void AddChild( shPtrA& child); accept shPtrA , shPtrB and shPtrC . You mentioned boost::static_pointer_cast. I will investigate. Thanks again. Brad
On 9/7/07, Peter Dimov
Brad Ryder:
...
typedef boost::shared_ptr<A> shPtrA; typedef boost::shared_ptr<B> shPtrB; typedef boost::shared_ptr<C> shPtrC;
class A {
void AddChild(shPtrA& child);
How about
void AddChild(shPtrA const& child);
? _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Peter, Thanks for helping me out here. You can see in my previous post, I am still having difficulty. Brad
participants (3)
-
Brad Ryder
-
Michael Fawcett
-
Peter Dimov