473,396 Members | 1,849 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.

Abstract Static Methods

I know that virtual static methods have been addressed, and I'm
not really
looking for virtual methods, because I don't really need the run
time dispatching.
What I would like is to have compile time enforcement of the
definition of
a couple of static methods on all classes that inherit from a
certain
class. Unfortunately I haven't quite stumbled upon it. Even a
static assert
would be better than nothing.

The best I've been able to come up with is:

std::list<bool (*)(void)> callbacks;

template <typename T>
struct A {

template <typename Y>
struct static_init {
static_init(void) { }
// just ignore for the moment that this returns void :)
static const bool registered = callbacks.push_back(
&Y::c1 );
};

static_init<T> stupid;
};

struct B : public A<B> {

static bool c1(void);
};

struct C : public A<C> {

static bool c1(void);
};

bool B::c1(void)
{
return true;
}

bool C::c1(void)
{
return false;
}

which doesn't compile due to the incomplete types that B and C
are by the
time the call to c1 is compiled. This is on g++ 3.2.3.

As you can see, what I'm trying to do is for any class
instantiated, I would like the program to add a static method of
that class to a list of callbacks before the object is actually
created. Any ideas on implementing this would be appreciated.

Jul 22 '05 #1
1 2420
"Icosahedron" <no***@nowhere.com> wrote...
I know that virtual static methods have been addressed, and I'm
not really
looking for virtual methods, because I don't really need the run
time dispatching.
What I would like is to have compile time enforcement of the
definition of
a couple of static methods on all classes that inherit from a
certain
class. Unfortunately I haven't quite stumbled upon it. Even a
static assert
would be better than nothing.


I don't know any mechanism in C++ (besides pure virtual function)
that would force a derived class to do anything, like implementing
a function. That's why the most often recommended path is to
thoroughly document what you need the user of your class to do to
change the default behaviour and actually provide that default
behaviour. Since your callbacks return either true or false, make
the default one return true and those classes who want to override
that will return false.

V
Jul 22 '05 #2

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

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
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: Sunny | last post by:
Hi again, in the past I have posted here a problem with static methods and abstract classes, and Jon Skeet and Richard Lowe have helped me to clarify the things. But now I have found another...
19
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this...
2
by: timpera2501 | last post by:
I am a newb to OOP programming and I'm only just starting with C#. I've done a TON of reading lately, and I'm currently in the process of modifying some of the function provided by the...
5
by: pali | last post by:
Hi, I was just wondering, why is not possible to make a member of a class BOTH abstract and static? MSDN says just that's in an error, not why this is so. In a nutshell, I need this: I have an...
7
by: Brybot | last post by:
Apparently it is not possible for a static class to extend an abstract class? I was wondering how else I might be able to go about my problem here? I have a base class Parent which has a static...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
2
by: pinki panda | last post by:
i want to knw the diff between static class and abstract class .i would appreciate if its followed by example
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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,...

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.