Thread: Is there a way to "force" a sub-class to have a static function?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Is there a way to "force" a sub-class to have a static function?

    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.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 6tr6tr View Post
    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?
    Don't define the method at all in the base class. Code won't compile if the particular class doesn't supply the method, right?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can't have static virtual functions.

    brewbuck, did you miss the static part?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    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:

    Code:
    template <typename C>
    void foo()
    {
        C::someStaticMethod();
    }
    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...

    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"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... but "method" means "virtual function" in C++, but that might not be what 6tr6tr intended to mean.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    hmm... but "method" means "virtual function" in C++, but that might not be what 6tr6tr intended to mean.
    Yeah, I'm being sloppy with terminology. Given 6tr6tr's admitted Java background I interpreted it in that context.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    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
    };

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM