I know this is probably impossible but is there any way to force all sub-classes of a certain class to have a static method? Either to make them implement one (like in pure virtual functions) or have the compiler complain they don't have it?
I know this is probably impossible but is there any way to force all sub-classes of a certain class to have a static method? Either to make them implement one (like in pure virtual functions) or have the compiler complain they don't have it?
Last edited by 6tr6tr; 05-04-2008 at 10:40 AM.
You can't have static virtual functions.
brewbuck, did you miss the static part?
No, I didn't miss it. I didn't suggest making it virtual. I was envisioning something like:
Which of course will not compile if the method doesn't exist for class C. In other words, my answer boils down to "You don't have to do anything special," but as usual I'm clueless as to what 6r6tr is actually trying to do here...Code:template <typename C> void foo() { C::someStaticMethod(); }
For instance, if he's trying to invoke a static method through a base object pointer and have the dispatch be polymorphic, then my response would be "the method cannot be static"
hmm... but "method" means "virtual function" in C++, but that might not be what 6tr6tr intended to mean.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So, 6tr, what are you actually trying to do? Because your question indicates that you're going about it all wrong - there is no sane use case for requiring a subclass to have a static function. (There is a use case for requiring that some class - usually a template parameter - has one, but that's different.)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Sorry, I was unclear. Brewbuck has what I want to do with the templated function. The problem is that while the base class that defines the templated function will not have the static function, the classes used with the templated function might have a base class "between" them and the template-function class that does. Example:
Code:class Base { public: template <typename T> a() { T::staticFunc(); } }; class Middle : public Base { public: static void staticFunc() { ...; } }; class Derived : public Middle { public: //Person forgot to put static function in };
So it's basically the same issue as the every-derived-needs-the-method problem of things like cloning and the visitor pattern.
There's no way to ensure the presence of these functions. But some people say that in such hierarchies, only the leaf classes should be concrete (and thus provide the function) - in other words, they say that it is a design error that your Middle class provides staticFunc().
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law