shared_ptr and inheritance
Hello
I have the following scenario:
#include <iostream>
#include
At 2:56 AM +0000 12/9/01, rz0 wrote:
#include <iostream> #include
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; } };
class Holder { boost::shared_ptr<A> aptr; public: void setclass(boost::shared_ptr<A> a) { aptr = a; } void callfunc() { aptr->func1(); } };
int main() {
Holder holder;
boost::shared_ptr<B> b(new B()); boost::shared_ptr<C> c(new C());
holder.setclass(b); //<------- holder.setclass(c); //<-------
holder.callfunc();
return 0; }
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>? -- Jon Kalb Kalb@LibertySoft.com
--- In Boost-Users@y..., Jon Kalb
At 2:56 AM +0000 12/9/01, rz0 wrote:
#include <iostream> #include
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; } };
class Holder { boost::shared_ptr<A> aptr; public: void setclass(boost::shared_ptr<A> a) { aptr = a; } void callfunc() { aptr->func1(); } };
int main() {
Holder holder;
boost::shared_ptr<B> b(new B()); boost::shared_ptr<C> c(new C());
holder.setclass(b); //<------- holder.setclass(c); //<-------
holder.callfunc();
return 0; }
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
At 12:06 AM +0000 12/10/01, rz0 wrote:
--- In Boost-Users@y..., Jon Kalb
wrote: At 2:56 AM +0000 12/9/01, rz0 wrote:
#include <iostream> #include
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; } };
class Holder { boost::shared_ptr<A> aptr; public: void setclass(boost::shared_ptr<A> a) { aptr = a; } void callfunc() { aptr->func1(); } };
int main() {
Holder holder;
boost::shared_ptr<B> b(new B()); boost::shared_ptr<C> c(new C());
holder.setclass(b); //<------- holder.setclass(c); //<-------
holder.callfunc();
return 0; }
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.
Your original code worked for me, but I'm using CodeWarrior. I noticed that the function that you want to have called: template<typename Y> shared_ptr(const shared_ptr<Y>&); is #ifdef'ed on BOOST_MSVC6_MEMBER_TEMPLATES, but I don't know what that define means. -- Jon Kalb Kalb@LibertySoft.com
participants (2)
-
Jon Kalb
-
rz0