I get the following error:
E:\C\main.cpp:173: error: request for member `retain' in `foo',
which is of non-class type `Pointer<Foo> ()(Foo
(*)())'
E:\C\main.cpp:174: error: request for member `release' in `foo',
which is of non-class type `Pointer<Foo> ()(Foo
(*)())'
Which is rather strange if you consider the following code:
template <class T>
class Pointer
{
public:
Pointer(T instance = T())
{
_pointer = new T(instance);
}
Pointer(T *instance)
{
_pointer = new T(*instance);
}
~Pointer()
{
release();
}
// shouldn't use this one really
T *pointer()
{
return _pointer;
}
// get the pointer, increase the count
T *retain()
{
if (_pointer)
{
_pointer->references.gain();
}
return _pointer;
}
// decrease the count
void release()
{
if (_pointer && !_pointer->references.lose())
{
delete _pointer;
_pointer = NULL;
}
}
private:
T *_pointer;
};
////////////////////////////////////////////////////////////////
class Foo
{
// irrelevant
};
int main()
{
Pointer<Foo> foo(Foo()); // creates a new instance of Foo
foo.retain(); // increases counter
foo.release(); // decreases counter
// pointers life time ends -> counter decreased -> Foo is deleted
}
Sure, it works if I do 'Pointer<Foo> foo;' but what is above should do exactly the same, that is, constructing a default Foo object. This is a test code, and the Foo's constructor would in almost all case accept parameters. Preferably I'd like this on one line. The following 2 example _DO_ work:
int main()
{
Pointer<Foo> foo1;
Foo tmp(/* parameters*/);
Pointer<Foo> foo2(tmp);
}
Why does it give this weird error? It looks like it takes it as a function pointer...?
PS: I can't (de)allocate (new / delete) in the main() function since the library could be compiled as a DLL, meaning I have to allocate on the lib and deallocate on the lib (or on the executable of course, but I choose the former).
Thanks!