473,412 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

named override in same class -- is it possible?

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?
May 30 '06 #1
2 1401
"Ben Voigt" <rb*@nospam.nospam> wrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...
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)
{
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?

May 30 '06 #2
Hi Ben,

"Ben Voigt" <rb*@nospam.nospam> wrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...
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
May 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
2
by: Tony Maresca | last post by:
Don't know if there are any Delphi/Object Pascal converts here, but being one, I sorely miss the feature that permits class (e.g., static) methods to be virtual and be overridden in derived...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
11
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
6
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a moderately complex class (about 10 different string and int type fields along with two different List<stringfields). I want to override Equals(), so I need to override GetHashCode()....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.