Search code examples
c++castingshared-ptr

type casting when return from function with shared_ptr


std::shared_ptr<AbstractPrinter> parse_input_fiz(char const *input) {
    std::shared_ptr<FizPrinter> printer(FizPrinter);

    return printer;
}

FizPrinter inherited from AbstractPrinter. And i got next error:

Description Resource Path Location Type could not convert ‘printer’ from ‘std::shared_ptr (*)(FizPrinter)’ to ‘std::shared_ptr’ parser.cc /pdf-i/src line 63 C/C++ Problem


this function one of used in Factory


Solution

  • Did you mean

    std::shared_ptr<FizPrinter> printer(new FizPrinter());
    

    Your current code declares a function and the constructor for a shared_ptr takes a pointer.