|
Boost Users : |
From: Dave Slutzkin (daveslutzkin_at_[hidden])
Date: 2006-11-20 01:50:29
On Sun, 19 Nov 2006 17:47:55 +0000, "Martin Waller"
<martinej.waller_at_[hidden]> said:
> Implementation #1:
> MyClass::MyClass()
> {
> init();
> }
>
> void MyClass::init()
> {
> spOther(new someOtherClass(this));
> }
>
> Implementation #2:
> MyClass::MyClass():spOther(new someOtherClass(this))
> {
> }
>
>
> Question:
>
> Why does implementation #2 fail to compile with the error:
>
> error: no match for call to
> '(boost::shared_ptr<someOtherClass>)(someOtherClass*)' ?
>
> yet implementation #2 compiles fine?
>
> Please help me understand what going on here...is it possible to get
> implementation #2 to compile or is it 'bad code'?
(Your references are mixed up here - I'm assuming implementation #1 is
the problematic one.)
Implementation #2 should work - you're using the constructor's
initialiser list to assign to the spOther member, using the shared_ptr's
constructor.
Implementation #1 attempts to call a method of the shared_ptr type with
a parameter of type someOtherClass* (as returned by new). shared_ptr
doesn't have any method matching that signature so the compiler gives an
error.
A more correct implementation of init would be:
void MyClass::init()
{
// either
spOther = shared_ptr<someOtherClass>(new someOtherClass(this));
// or
spOther.reset(new someOtherClass(this));
}
Dave.
-- Dave Slutzkin Melbourne, Australia daveslutzkin_at_[hidden]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net