jko
jko

Reputation: 125

shared_ptr : "call of an object of a class type without appropriate operator() or conversion function to pointer-to-function type"

#include "boost\shared_ptr.hpp"

class A{

public:
A(){}
~A(){}

};

int main()
{
    boost::shared_ptr<A> ptrA;
    ptrA(new A); 

}

I would like to know why this code won't compile? I want to know the difference if I just use

boost::shared_ptr<A> ptrA(new A);?

Upvotes: 1

Views: 45252

Answers (1)

Karthik T
Karthik T

Reputation: 31972

boost::shared_ptr<A> ptrA(new A);

Calls conversion constructor which converts A* into the shared_ptr. This is a default way to construct the ptr.

ptrA(new A); 

Calls operator(). This is used for a lot of reasons, one being to make objects emulate functions, i.e functors. But this is not used with shared_ptr.

The constructor exists, operator() doesnt.

Upvotes: 4

Related Questions