[Shared_ptrs] As I doing this right
Although I have been using shared_ptr's for a while, I still get a bit confused with polymorphic stuff ...no change there. An pseudocode example: Class A { }; Class B : public A { DoStuff1(); DoStuff2(); DoStuff3(); DoStuff4() }; // MyFunction handles A and classes derived from A MyFunction(shared_ptr<A>) { }; Main() { shared_ptr<A> poly<new B); // OK so far MyFunction(poly); // Still OK - // OK, done this, but now if I want to call a load of stuff in an instance of B // I either have to make a load of pointer casts (because poly is an A) dynamic_pointer_cast<B>(poly)->DoStuff1(); dynamic_pointer_cast<B>(poly)->DoStuff2(); dynamic_pointer_cast<B>(poly)->DoStuff3(); dynamic_pointer_cast<B>(poly)->DoStuff4(); // Or this? shared_ptr<B> tmp = dynamic_pointer_cast<B>(poly); // is this right? // I have to create a new pointer? tmp->DoStuff1(); tmp->DoStuff2(); tmp->DoStuff3(); tmp->DoStuff4(); } So, the quesiton is one of efficiency - if I was using pointers, I would just new a B and assign to a B* variable, then pass in to the function which would use it as an A - no temp required. With above a new tmp pointer seems to be required - am I right or have I missed something blindingly obvious. James This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.
This is what I would do: Main() { shared_ptr<B> poly(new B); // Start with a pointer that matches the type MyFunction( dynamic_pointer_cast<A>(poly) ); // Cast it in the argument list } -- Windflower -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Hughes, James Sent: Tuesday, May 01, 2007 4:19 AM To: Boost-users@lists.boost.org Subject: [Boost-users] [Shared_ptrs] As I doing this right Although I have been using shared_ptr's for a while, I still get a bit confused with polymorphic stuff ...no change there. An pseudocode example: Class A { }; Class B : public A { DoStuff1(); DoStuff2(); DoStuff3(); DoStuff4() }; // MyFunction handles A and classes derived from A MyFunction(shared_ptr<A>) { }; Main() { shared_ptr<A> poly<new B); // OK so far MyFunction(poly); // Still OK - // OK, done this, but now if I want to call a load of stuff in an instance of B // I either have to make a load of pointer casts (because poly is an A) dynamic_pointer_cast<B>(poly)->DoStuff1(); dynamic_pointer_cast<B>(poly)->DoStuff2(); dynamic_pointer_cast<B>(poly)->DoStuff3(); dynamic_pointer_cast<B>(poly)->DoStuff4(); // Or this? shared_ptr<B> tmp = dynamic_pointer_cast<B>(poly); // is this right? // I have to create a new pointer? tmp->DoStuff1(); tmp->DoStuff2(); tmp->DoStuff3(); tmp->DoStuff4(); } So, the quesiton is one of efficiency - if I was using pointers, I would just new a B and assign to a B* variable, then pass in to the function which would use it as an A - no temp required. With above a new tmp pointer seems to be required - am I right or have I missed something blindingly obvious. James This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Hughes, James Sent: May 1, 2007 5:19 AM To: Boost-users@lists.boost.org Subject: [Boost-users] [Shared_ptrs] As I doing this right
Although I have been using shared_ptr's for a while, I still get a bit confused with polymorphic stuff ...no change there.
An pseudocode example:
Class A { };
Class B : public A { DoStuff1(); DoStuff2(); DoStuff3(); DoStuff4() };
This is not polymorphic. To make this example polymorphic you need to declare the functions virtual in class A.
So, the quesiton is one of efficiency - if I was using pointers, I would just new a B and assign to a B* variable, then pass in to the function which would use it as an A - no temp required.
This requires that the functions be declared in A. If you do that, your example will work with either raw pointers or shared_ptr's. Jules.
I should have been a bit more specific - let's say that the DoStuff functions in B are in addition to any virtual functions there may be in A (which make it polymorphic and were omitted in the example). This is why I do need to be able to call specific functions in B, but still need to stored or passed to functions as the base class pointer A. Hence the issue. James -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Jules d'Entremont Sent: 01 May 2007 15:27 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Shared_ptrs] As I doing this right
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Hughes, James Sent: May 1, 2007 5:19 AM To: Boost-users@lists.boost.org Subject: [Boost-users] [Shared_ptrs] As I doing this right
Although I have been using shared_ptr's for a while, I still get a bit
confused with polymorphic stuff ...no change there.
An pseudocode example:
Class A { };
Class B : public A { DoStuff1(); DoStuff2(); DoStuff3(); DoStuff4() };
This is not polymorphic. To make this example polymorphic you need to declare the functions virtual in class A.
So, the quesiton is one of efficiency - if I was using pointers, I would just new a B and assign to a B* variable, then pass in to the function
which would use it as an A - no temp required.
This requires that the functions be declared in A. If you do that, your example will work with either raw pointers or shared_ptr's. Jules. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.
participants (3)
-
Hughes, James
-
Jules d'Entremont
-
Windflower Waters