473,412 Members | 2,239 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.

Workaround for static methods that need to be virtual

K
Hi all; I don't think this is a VC compiler question, so I am asking this
here. I did hunt around the web and Stroustrup before posting, but found no
references to this problem.

I have a base class (BasePort) that defines an interface for subclasses
(LPort, NPort). Each implementation of BasePort as LPort or NPort requires
a default configuration, which is currently obtained via a static method,
GetDefaultConfiguration() (henceforth, GDC). BasePort implements that
method to always return false, in the hopes that it would be obvious to
*Port implementers that something is _wrong_ (it's documented, too).
However, attempts to use LPort or Nport's GDC go to BasePort's GDC, not the
*Port GDC.

So, a bit of thinking here, implies that the BasePort GDC is not virtual so
dynamic binding can't be done, and the base GDC is called, not the sub.
But, it appears that static methods can't be virtual, so redirection seems
problematic.

The obvious solution is to make the methods non-static, and then virtual;
however, the *Port implementations depend on licenses that I do not want to
tie up just to get configurations (plus, the creation of the objects is
fairly expensive) because the licenses are very expensive ($) and needlessly
eating one is an issue.

Another solution is to provide a register method in the parent class, and
each *Port registers it's own GetDefaultConfiguration with BasePort and have
the BasePort GDC resolve the right one. This seems... inelegant.

So, better solutions? Anyone have an idea on how to handle this an C++ OO
way?

Thanks in advance!
--Keith
Jul 22 '05 #1
2 1433


K wrote:
So, better solutions? Anyone have an idea on how to handle this an C++ OO
way?


You might check out the example for the Acyclic Visitor Pattern at the
following url:

http://www.objectmentor.com/resources/articles/acv.pdf
Jul 22 '05 #2
K wrote:
Hi all; I don't think this is a VC compiler question, so I am asking this
here. I did hunt around the web and Stroustrup before posting, but found no
references to this problem.

I have a base class (BasePort) that defines an interface for subclasses
(LPort, NPort). Each implementation of BasePort as LPort or NPort requires
a default configuration, which is currently obtained via a static method,
GetDefaultConfiguration() (henceforth, GDC). BasePort implements that
method to always return false, in the hopes that it would be obvious to
*Port implementers that something is _wrong_ (it's documented, too).
However, attempts to use LPort or Nport's GDC go to BasePort's GDC, not the
*Port GDC.

So, a bit of thinking here, implies that the BasePort GDC is not virtual so
dynamic binding can't be done, and the base GDC is called, not the sub.
But, it appears that static methods can't be virtual, so redirection seems
problematic.

The obvious solution is to make the methods non-static, and then virtual;
however, the *Port implementations depend on licenses that I do not want to
tie up just to get configurations (plus, the creation of the objects is
fairly expensive) because the licenses are very expensive ($) and needlessly
eating one is an issue.

Another solution is to provide a register method in the parent class, and
each *Port registers it's own GetDefaultConfiguration with BasePort and have
the BasePort GDC resolve the right one. This seems... inelegant.

So, better solutions? Anyone have an idea on how to handle this an C++ OO
way?


If registering with the base class is "inelegant", then my suggestion
may seem inelegant too, but I'll give it nonetheless. It is what is
known as "policy-based design":

template<class DefConfKeeper> // policy - how to get def conf
class BasePort {
// ...
public:
static bool GetDefaultConfiguration(Configuration& c) {
return DefConfKeeper::GetDefaultConfiguration(c);
}
};

struct LPortDefConfKeeper {
static bool GetDefaultConfiguration(Configuration& c) {
// do something with 'c'
return true;
}
};

class LPort : public BasePort<LPortDefConfKeeper> {
...
};

struct NPortDefConfKeeper {
static bool GetDefaultConfiguration(Configuration& c) {
// do something with 'c'
return true;
}
};

class NPort : public BasePort<NPortDefConfKeeper> {
...
};

This solution is not necessarily "OO", but it's certainly "C++".

Victor
Jul 22 '05 #3

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

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
4
by: MaxMax | last post by:
Now... I have a problem... It's an engineering problem. I have a function, we will call it MyBigFunc. It's a function that can be easily built as a static method, because it is the only function...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
3
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: d-42 | last post by:
Hi, Is there any way to make this method inheritable and have it behave appropriately on inherited classes? Using generics? Extension methods? Something else? class baseClass { public...
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.