473,699 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,
GetDefaultConfi guration() (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 GetDefaultConfi guration 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 1444


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,
GetDefaultConfi guration() (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 GetDefaultConfi guration 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 GetDefaultConfi guration(Config uration& c) {
return DefConfKeeper:: GetDefaultConfi guration(c);
}
};

struct LPortDefConfKee per {
static bool GetDefaultConfi guration(Config uration& c) {
// do something with 'c'
return true;
}
};

class LPort : public BasePort<LPortD efConfKeeper> {
...
};

struct NPortDefConfKee per {
static bool GetDefaultConfi guration(Config uration& c) {
// do something with 'c'
return true;
}
};

class NPort : public BasePort<NPortD efConfKeeper> {
...
};

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
33177
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 to work before I added the changes in, and I'm not sure if that's causing it.
11
4604
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
12
13456
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 of your Derived classes will need to implement some method, but implement it differently, and that the base class cannot implement it. This is where pure virtual comes in.
4
2757
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 that the "user" will use and it is stateless. So I wrote something like: class MyClass { public: static int MyBigFunc() { return 0;}
33
3335
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
3
6650
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
17
2348
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
9
5847
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
14
1631
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 static string ClassName() {
0
9187
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8936
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8894
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7776
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.