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

This is a discussion on Is there a way to "force" a sub-class to have a static function? within the C++ Programming forums, part of the General Programming Boards category; I know this is probably impossible but is there any way to force all sub-classes of a certain class to ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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,317
    You can't have static virtual functions.

    brewbuck, did you miss the static part?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    21,415
    hmm... but "method" means "virtual function" in C++, but that might not be what 6tr6tr intended to mean.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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,893
    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,893
    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, 12:03 PM
  5. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21