Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2002-03-24 15:05:24


On Sunday, March 24, 2002, at 10:41 AM, David B. Held wrote:

> I have something like this:
>
> class Base
> {
> public:
> virtual void foo();
> };
>
> class Derived
> {
> public:
> virtual void foo();
> void bar();
> };

In this example, you forgot to have Derived inherit from Base. Change
this to:

      class Derived : public Base

and everything should be fine.

I turned your example into this test program, and it worked.

     #include <boost/shared_ptr.hpp>
     #include <iostream>
     #include <vector>

     class Base
     {
     public:
         virtual void foo() { }
     };

     class Derived : public Base
     {
     public:
         virtual void foo() { std::cout << "Derived::foo called\n"; }
         void bar();
     };

     typedef boost::shared_ptr<Base> PBase;
     typedef boost::shared_ptr<Derived> PDerived;

     int main()
     {
         std::vector<PBase> BaseObjs;
         std::vector<PDerived> DerivedObjs;

         PDerived p(new Derived());

         BaseObjs.push_back(p);
         DerivedObjs.push_back(p);

         BaseObjs[0]->foo();
     }

I think there's no problem with shared_ptr here.

     -- Darin


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk