473,396 Members | 2,147 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,396 software developers and data experts.

more inheritance patterns

Hello,

At work, someone showed me a way to avoid virtual functions while
maintaining an interface class.

Basically,

class _MyClass {
void Method;
};

#if defined(LINUX)
#include "linux/MyLinuxClass.h"
class MyClass : public MyLinuxClass {};

#elif defined(WINDOWS)
#include "windows/MyWindowsClass.h"
class MyClass : public MyWindowsClass {};
#endif

Then, throughout the code base, MyClass would be used, following the
interface defined in _MyClass, while still allowing platform-specific
code where needed.

My question is, where could I read about more constructions like this?
My coworker called it an "inheritance pattern." I'd love to learn more
about them (even though I'm sure a case can be made for avoiding this
style).

Thanks much,
Mick Charles Beaver

Jun 27 '08 #1
7 1036
Alf P. Steinbach <al***@start.nowrote:
>This is a programmer enforced design, which requires never using a
pointer to the "abstract" base class. It's a high-performance
requirement, where virtual functions have too high of an overhead and
must be avoided.

Have you measured, and if so, what were the results?
I don't have any hard numbers at my disposal, but on the particular
architecture where it is an issue, virtual functions almost always
result in a branch misprediction.
#ifdef-based code is a point number 4.

It seems you folks go out of your way to create an unmaintainable mess. :-(

If was weened off #ifdef-based code in 1985 or thereabouts. You don't need it.
It's stupid.
I believe it has its place. Maintainability is always a goal, so this
construct and its associated #ifdef's are used sparingly.

-Mick

Jun 27 '08 #2
On Jun 27, 7:06 pm, m...@cs.wisc.edu (Mick Charles Beaver) wrote:
Alf P. Steinbach <al...@start.nowrote:
This is a programmer enforced design, which requires never using a
pointer to the "abstract" base class. It's a high-performance
requirement, where virtual functions have too high of an overhead and
must be avoided.
Have you measured, and if so, what were the results?
I don't have any hard numbers at my disposal, but on the particular
architecture where it is an issue, virtual functions almost always
result in a branch misprediction.
Sounds like HP PA. Still what you're hiding here is system
dependencies, i.e. system calls. Even a branch misprediction is
negligible compared to the context switch to system mode, and
everything which goes on there.
#ifdef-based code is a point number 4.
It seems you folks go out of your way to create an
unmaintainable mess. :-(
If was weened off #ifdef-based code in 1985 or thereabouts.
You don't need it. It's stupid.
I believe it has its place.
I has its place, yes. Obfuscation and job security, for
example. Other than that, any #if other than an include guard
is really serious enough to get a programmer fired, at least if
I'm in charge.
Maintainability is always a goal, so this construct and its
associated #ifdef's are used sparingly.
Actually, as you presented it, the construct doesn't need the
#if's at all. You just have to organize your code a little
better. (But of course, as you presented it, the construct just
added a lot of code for nothing anyway.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
On Jun 27, 6:17 pm, Noah Roberts <u...@example.netwrote:
Mick Charles Beaver wrote:
At work, someone showed me a way to avoid virtual functions
while maintaining an interface class.
Basically,
class _MyClass {
void Method;
};
#if defined(LINUX)
#include "linux/MyLinuxClass.h"
class MyClass : public MyLinuxClass {};
#elif defined(WINDOWS)
#include "windows/MyWindowsClass.h"
class MyClass : public MyWindowsClass {};
#endif
Then, throughout the code base, MyClass would be used,
following the interface defined in _MyClass, while still
allowing platform-specific code where needed.
I don't understand, are you saying that you cast your MyClass
objects to _MyClass? I don't see how you could otherwise be
getting any behavior from your MyClass objects at all. The
subclassing thing I understand but I don't see what _MyClass
is doing.
Frankly, the simplest way to implement non-virtual
polymorphism is with templates.
Even that's unnecessary additional complexity (and not a very
good solution for this particular problem). In this case, link
time resolution handles all of the problems simply and
elegantly.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
Even that's unnecessary additional complexity (and not a very
good solution for this particular problem). In this case, link
time resolution handles all of the problems simply and
elegantly.
Would you be able to recommend any web resources for learning more about
link time resolution?

Thank you
Jun 27 '08 #5
Mick Charles Beaver wrote:
>Even that's unnecessary additional complexity (and not a very
good solution for this particular problem). In this case, link
time resolution handles all of the problems simply and
elegantly.

Would you be able to recommend any web resources for learning more about
link time resolution?

Thank you
I never said what you are quoting above. Who did? Did you reply to the
posting you intended to?
Jun 27 '08 #6
Alf P. Steinbach wrote:
* Mick Charles Beaver:
>Alf P. Steinbach <al***@start.nowrote:
>> 1. Name that starts with underscore followed by uppercase, "_MyClass",
is reserved for the implementation.

I agree that should change.
>> 2. Non-virtual member functions can't be overridden.

This is a programmer enforced design, which requires never using a
pointer to the "abstract" base class. It's a high-performance
requirement, where virtual functions have too high of an overhead and
must be avoided.

Have you measured, and if so, what were the results?

It's interesting if you have done so, because to the best knowledge
available virtual functions have near to zero overhead.
Well, they certainly won't be inlined.
Jun 27 '08 #7
Noah Roberts <us**@example.netwrote:
I never said what you are quoting above. Who did? Did you reply to the
posting you intended to?
Yet another good reason to attribute a quote to someone.

Ugh.

-Mick
Jun 27 '08 #8

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

Similar topics

14
by: Rene | last post by:
Its been around 1.5 years now since I started using C#, I have already read the official "Beginning C#" type of books and another book that uses C# to explain object oriented programming concepts....
0
by: headware | last post by:
In his book "Patterns of Enterprise Application Architecture", Martin Fowler says that using the Table Module pattern to model the domain logic precludes the use of inheritance and GoF style OO...
6
by: DKode | last post by:
i'm a little confused as how to accomplish this I have multiple actors in a timesheet system I am developing. Some of the actors are: Administrator, Manager, Reviewer, DefaultUser, etc... Now,...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
8
by: query_me2001 | last post by:
I have a queue that holds a list of objects with a common base type. As a simple and contrived example, I have a common type of transportation methods and derived classes (with there own individual...
21
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
6
by: Manan | last post by:
Hi, i'm new to OOP concept. in college days i have read lots of good things about "inheritance" but does any one uses this concept in real life project ? or can some one tell me when it will be...
14
by: karthikbalaguru | last post by:
Hi, What could be the maximum Accepted Layers/Levels of inheritance in a normal C++ program that has private(data),protected(data) and public(data,member functions) ? On what does this depend...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.