I have just been introduced to boost and shared_ptr and having a few difficulties getting it to work with template functions and inheritence
I have the following classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Object
{
public:
typedef boost::shared_ptr<Object> Ptr;
};
class MyObject : public Object
{
public:
typedef boost::shared_ptr<MyObject> Ptr;
};
class Service
{
template<typename T>
T::Ptr doSomething();
}
The problem arises when I do the following:
1 2
Service svc;
MyObject::Ptr result = svc.doSomething<MyObject>();
Because the typedef is defined in both Object and MyObject with the same name the compiler rightfully says the typedef is ambiguous, but in order to use the templated method they have to have the same name
My current solution is to remove the typedef from the base class Object and put it outside the scope of the class definition. I am not very happy about this solution since I can no longer do the following:
Object::Ptr result = svc.doSomething<Object>();
I assume this is a common problem and there is a simple solution?
You need to add typename on line 16 ( typename T::Ptr doSomething(); )
The derived typedef should shadow the base typedef, adding 'typename' as stated above compiles fine with g++