Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

boost::shared_ptr assignment

Hi,

I'm having problems assigning a boost::shared_ptr. The following code doesn't compile for me - what am i doing wrong. I have a base class, Foo which is extended to Boo and Zoo:

class Foo{
public:
Foo() {}
virtual ~Foo() {}
virtual void do_something() {/*..*/}
};

class Boo : public Foo{
public:
Boo () {}
virtual ~Boo () {}
virtual void do_something() {/*..*/}
};

class Zoo : public Foo {
public:
Zoo () {}
virtual ~Zoo () {}
virtual void do_something() {/*..*/}
};

At runtime I want to assign a Boo or Zoo pointer to my shared_ptr:

int main()
{
shared_ptr<Foo> pf;

if (...) {
pf = new Boo;
} else {
pf = new Zoo;
}
pf->do_something();
}

Note that I do NOT want to assign the shared_ptr at construction, i.e. using pf(new Foo) - I need to the assignment until runtime.

Thanks for your help.
[1062 byte] By [helix] at [2007-11-11 8:12:03]
# 1 Re: boost::shared_ptr assignment
What is the error message you're getting? Anyway, you can try to assign a temporary shared_ptr:

pf = boost::shared_ptr<Foo> (new Bar);
Danny at 2007-11-11 21:01:30 >