named override in same class -- is it possible?

B

Ben Voigt

I have a function that is called for several distinct reasons. It is
conceivable that a subclass would want to handle those cases differently.
Therefore, what I want to do is have multiple v-table slots, all pointing to
a common base function. These functions should be protected, not public.

Here is a short example of what I'm trying to accomplish:


class XYZ
{
protected:
virtual bool SupportsX();
virtual bool SupportsY();
virtual bool SupportsZ();

virtual bool NoSupport() = SupportsX, SupportsY, SupportsZ;
};

bool XYZ::NoSupport( void ) { return false; }

Except that in my case the common default function is considerably more
complicated, and I don't want to repeat the code everywhere.

The compiler gives me:
error C3653: 'SupportsX' : cannot be used as a named override: a function
being overridden not found; did you forget to name the function explicitly,
using a :: operator?

Right now I've worked around this by adding an extra inheritance level -- an
abstract base class that defines pure virtual functions that I can
explicitly override, but that really shouldn't be necessary, because there's
no technical reason the compiler can't introduce a new set of v-table slots
and fill them all with the same method pointer. Am I just missing the right
syntax?
 
B

Ben Voigt

Ben Voigt said:
I have a function that is called for several distinct reasons. It is
conceivable that a subclass would want to handle those cases differently.
Therefore, what I want to do is have multiple v-table slots, all pointing
to a common base function. These functions should be protected, not
public.

Here is a short example of what I'm trying to accomplish:


class XYZ
(I meant ref class)
 
M

Marcus Heege

Hi Ben,

Ben Voigt said:
I have a function that is called for several distinct reasons. It is
conceivable that a subclass would want to handle those cases differently.
Therefore, what I want to do is have multiple v-table slots, all pointing
to a common base function. These functions should be protected, not
public.

Here is a short example of what I'm trying to accomplish:


class XYZ
{
protected:
virtual bool SupportsX();
virtual bool SupportsY();
virtual bool SupportsZ();

virtual bool NoSupport() = SupportsX, SupportsY, SupportsZ;
};

bool XYZ::NoSupport( void ) { return false; }

Except that in my case the common default function is considerably more
complicated, and I don't want to repeat the code everywhere.

The compiler gives me:
error C3653: 'SupportsX' : cannot be used as a named override: a function
being overridden not found; did you forget to name the function
explicitly, using a :: operator?

Right now I've worked around this by adding an extra inheritance level --
an abstract base class that defines pure virtual functions that I can
explicitly override, but that really shouldn't be necessary, because
there's no technical reason the compiler can't introduce a new set of
v-table slots and fill them all with the same method pointer. Am I just
missing the right syntax?
To use named overriding, there must be a vertual function in the base class.
Therefore, you can do this only with the workaround you have descibed.
However, I don't see why you don't implement stubs for all functions.

virtual bool SupportsX() { return false; }
virtual bool SupportsY() { return false; }
virtual bool SupportsZ() { return false; }

Marcus
 

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

Top