Re: Templates and derivation
Manfred von Willich wrote:
Lars Jordan wrote:
My question is. Are there other solutions which solve the problem from described
above? Is there a solution which avoids the usage of such a ImplType-like member?
I have stripped out extras, leaving the minimum. What I have given
compiles successfully, even when using your Holder class. Essentially,
two lines change: the addition of a template operator to template class
MF, and the need for the explicit invocation of this operator to get
the compiler to recognise the conversion.
template<typename T> class MF : public T
{
public:
template<typename B> operator MF<B>& () { return *this; }
};
What was the compiler that you used? VC8 gives me the warning that the operator
is recursive on all control paths - which leads to a stack overflow. This is
something that I do not understand at all, since I can't see why the same
operator should be called again. If you could give me a hint why this happens
this would be great.
class Base
{
public:
virtual ~Base() { }
};
class Derived : public Base
{
public:
virtual ~Derived() { }
};
int main( int, char*[] )
{
MF<Derived>* pMFD = new MF<Derived>;
MF<Base>* pMFB = &(MF<Base>&)*pMFD;
return 0;
}
I do not know if it can be done without explicitly invoking a
conversion operator. I hope this is something like what you are
looking for.
I tried to call it without the explicit invocation of the operator but that
didn't work. It seems that this operator is not a viable option for the compiler
to select automatically for conversion in assignment.
I like the idea of using operators though this is a part of c++ that I do not
know enough about.
regards
Lars
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]