Smart pointers and polymorphism

Posted by qwerty on Stack Overflow See other posts from Stack Overflow or by qwerty
Published on 2010-04-21T11:48:41Z Indexed on 2010/04/21 11:53 UTC
Read the original article Hit count: 360

Filed under:
|
|
|
|

hello.
I implemented reference counting pointers (called SP in the example) and im having problems with polymorphism which i think i shouldn't have. In the following code:

    SP<BaseClass> foo()
    {   
        // Some logic...
        SP<DerivedClass> retPtr = new DerivedClass();
        return retPtr;
    }

DerivedClass inherits from BaseClass. With normal pointers this should have worked, but with the smart pointers it says "cannot convert from 'SP<T>' to 'const SP<T>&" and i think it refers to the copy constructor of the smart pointer.
How to i allow this kind of polymorphism with reference counting pointer?
I'd appreciate code samples cause obviously im doing something wrong here if im having this problem.

Thanks! :)

[p.s., plz don't tell me to use standart liberary with smart pointers cuz that's impossible at this moment.]

© Stack Overflow or respective owner

Related posts about c++

Related posts about smart