RE: [Boost-Users] Re: shared_ptr and inheritance
From: rz0 [mailto:rz0@yahoo.com]
VC++ 6.0 flags the above lines with a conversion error.
Is there any way to resolve this?
Can you just define b and c as boost::shared_ptr<A>?
Hi Jon
Thank you very much for the response. Unfortunately this will cause the same problem that I ran into with std::auto_ptr and that is methods funcB and funcC which are exclusive to B and C are not allowed to be called.
Take care RZ
It seems to me that the problem is in the design - you're trying to use inheritance for the convenience of placeholding (and maybe insulation). That won't work (as you've seen). Even with a compiler that allows the code to compile (those that support member templates will) you will not get the behaviour that you want - funcB and funcC can never be called (from client code). The reason that the code compiles cleanly is that the conversion from B* or C* to A* is valid - but there's no way you can dynamically change the static type of "boost::shared_ptr<A> aptr;". If you really need to get this to work, it is still possible: You could use boost::any, and any_cast through all the possible types. This, however, is not very flexible, and IMO not very readable or maintainable. Regards, Bjorn Karlsson [Non-text portions of this message have been removed]
--- In Boost-Users@y..., Björn Karlsson
It seems to me that the problem is in the design - you're trying to
inheritance for the convenience of placeholding (and maybe insulation). That won't work (as you've seen). Even with a compiler that allows the code to compile (those that support member templates will) you will not get
use the
behaviour that you want - funcB and funcC can never be called (from client code). The reason that the code compiles cleanly is that the conversion from B* or C* to A* is valid - but there's no way you can dynamically change the static type of "boost::shared_ptr<A> aptr;".
If you really need to get this to work, it is still possible: You could use boost::any, and any_cast through all the possible types. This, however, is not very flexible, and IMO not very readable or maintainable.
Regards,
Bjorn Karlsson
Hi Bjorn Thank you very much for the reply. Unfortunately these classes are provided to me and as part of the requirements, I must be able to work with them. In re-thinking the problem, it seems that I'm in desperate need of a polymorphic container - i.e.: class A { public: virtual void func1() { std::cout << "A::func1" << std::endl; } }; class B : public A { public: void func1() { std::cout << "B::func1" << std::endl; } void funcB() { std::cout << "B::funcB" << std::endl; } }; class C : public A { public: void func1() { std::cout << "C::func1" << std::endl; } void funcC() { std::cout << "C::funcB" << std::endl; } }; int main() { std::vector< some_smart_ptr<A> > vec; some_smart_ptr<B> b(new B()); b->funcB(); vec.push_back(b); } In the Boost group, Mr. Peter Dimov provided a library called variant. This worked exceptionally well for me on Win2k compiled by VC++ 6.0. However my target platform is Solaris 2.7 and the compiler is Sun C++ v6.0 compiler. Unfortunately the results on this platform were quite unexpected. Does boost have any polymorphic containers? Thanks very much in advance.
participants (2)
-
Björn Karlsson
-
rz0