accessing/overloading parent overloaded operators

N

NKOBAYE027

I want to use the * operator of the parent class in the def'n of the *
operator for my child class. Is there an elegant way to do this e.g.

template<typename object>
class foo : public pFoo<object>
{
// yadayadayada
object& operator*()
{
WhateverDereferencedParentReturns bar =
HowDoIGetTheReturnOfParentOperator*;
return SomeOperation(bar);
}
}

I think I can do a static_cast on the 'this' pointer to get 'bar'

e.g.

WhateverDereferencedParentReturns bar = *static_cast<pFoo>(*this);

but this seems clunky to me...is there a more elegant solution?

regards,
L.
 
J

John Harrison

NKOBAYE027 said:
I want to use the * operator of the parent class in the def'n of the *
operator for my child class. Is there an elegant way to do this e.g.

template<typename object>
class foo : public pFoo<object>
{
// yadayadayada
object& operator*()
{
WhateverDereferencedParentReturns bar =
HowDoIGetTheReturnOfParentOperator*;
return SomeOperation(bar);
}
}

I think I can do a static_cast on the 'this' pointer to get 'bar'

e.g.

WhateverDereferencedParentReturns bar = *static_cast<pFoo>(*this);

WhateverDereferencedParentReturns bar = *static_cast<pFoo<object>& >(*this);

or this

WhateverDereferencedParentReturns bar = **static_cast<pFoo<object>* >(this);

Personally I would use the second, I never entirely trust casting with
references. Your solution (when corrected for syntax) involves the creation
of a temporary pFoo said:
but this seems clunky to me...is there a more elegant solution?

Perhaps this

class foo : public pFoo<object>
{
typedef pFoo<object> base_type;
object& operator*()
{
WhateverDereferencedParentReturns bar = base_type::eek:perator*(*this);
return SomeOperation(bar);
}

I think that is the clearest.

John
 
N

NKOBAYE027

Thanks again, John. It was 311 am so I had to give up abusing my brain (or
the mush that it has undoubtedly been replaced with over the past years) and
get some sleep. :eek:) I feel much better now....

I like your sol'n to the casting issue better...I think my objections to it
were the same as yours...I'd rather cast pointers than objects or references
to them.

regards,
L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top