Smart Pointer Issue
Hello, I have the following classes:
class ServiceBase
{
public:
ServiceBase() {}
~ServiceBase() {}
void Stop() {}
};
class ServiceDerivate:public ServiceBase
{
public:
ServiceDerivate() {}
~ServiceDerivate() {}
};
And I have linked smart pointer class which, does not destroy, but
calls Stop method on its destruction:
template <class X> class ServicePtr
{
public:
explicit ServicePtr(X* p = 0) throw()
: itsPtr(p) {itsPrev = itsNext = this;}
~ServicePtr()
{release();}
ServicePtr(const ServicePtr& r) throw()
{acquire(r);}
ServicePtr& operator=(const ServicePtr& r)
{
if (this != &r) {
release();
acquire(r);
}
return *this;
}
X& operator*() const throw() {return *itsPtr;}
X* operator->() const throw() {return itsPtr;}
X* get() const throw() {return itsPtr;}
bool unique() const throw() {return itsPrev ? itsPrev==this :
true;}
private:
X* itsPtr;
mutable const ServicePtr* itsPrev;
mutable const ServicePtr* itsNext;
void acquire(const ServicePtr& r) throw()
{ // insert this to the list
itsPtr = r.itsPtr;
itsNext = r.itsNext;
itsNext->itsPrev = this;
itsPrev = &r;
r.itsNext = this;
}
void release()
{ // erase this from the list, delete if unique
if (unique())
itsPtr->Stop();
else {
itsPrev->itsNext = itsNext;
itsNext->itsPrev = itsPrev;
itsPrev = itsNext = 0;
}
itsPtr = 0;
}
};
Since ServiceBase and ServiceDerivated have to be dynamic castable i
wish I could use a pool of ServiceBase classes, and upon request to
spawn new ServicePtr holding dynamic_cast to ServiceBase. Sipmle
example:
int main()
{
ServicePtr<ServiceBase> sbase;
ServicePtr<ServiceDerivate> sderivate;
sderivate=sbase;
return 0;
}
which, produces:
"no matching function for call to
'ServicePtr<ServiceDerivate>::operator=(ServicePtr<ServiceBase>&)'
note: candidates are: ServicePtr<X>& ServicePtr<X>::operator=(const
ServicePtr<X>&) [with X = ServiceDerivate]
Can anybody help with this. Probably I am missing something.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]