the template code is like this:
template <class type1>
struct DefaultInstanceCreator {
type1 * operator ()() {
return new type1;
}
};
template < class type1
, class InstanceCreator = DefaultInstanceCreator<type1> >
class objectCache
{
public:
objectCache (InstanceCreator & instCreator)
:instCreator_ (instCreator) {}
type1* Get() {
type1 * temp = instCreator_ ();
}
private:
InstanceCreator instCreator_;
};
this code work well with object class like this:
class A{
public:
A(int num){
number = num;
}
int number;
struct CreateInstance {
CreateInstance (int value) : value_ (value) {}
A * operator ()() const{
return new A(value_);
}
int value_;
};
};
objectCache< A, A::CreateInstance > intcache(A::CreateInstance(2));
A* temp = intcache.Get();
cout << temp->number <<endl;
when I tried this template with type like int, string...
objectCache< int > intcache();
int* temp = intcache.Get();
*temp = 3;
cout <<temp <<endl;
I get E left of "'.Get' must have class/struct/union", I can't find out where is the problem
when I change to
objectCache< int > intcache;
I get "'objectCache' : no appropriate default constructor available"
use
objectCache< int > intcache(DefaultInstanceCreator<int>());
I get left of "'.Get' must have class/struct/union" too.