Abstract Static Methods

Discussion in 'C++' started by Icosahedron, Jan 30, 2004.

  1. Icosahedron

    Icosahedron Guest

    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.
     
    Icosahedron, Jan 30, 2004
    #1
    1. Advertising

  2. "Icosahedron" <> 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
     
    Victor Bazarov, Jan 30, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    4
    Views:
    552
    Lee Weiner
    Apr 20, 2004
  2. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    534
    Iyer, Prasad C
    Oct 20, 2005
  3. Gerald Klix

    Re: Abstract Methods & Abstract Class

    Gerald Klix, Oct 20, 2005, in forum: Python
    Replies:
    1
    Views:
    345
    =?iso-8859-1?B?c+liYXN0aWVu?=
    Oct 20, 2005
  4. ankur

    Abstract Static methods

    ankur, Nov 25, 2007, in forum: Java
    Replies:
    5
    Views:
    620
    ankur
    Nov 26, 2007
  5. Tomas Mikula

    abstract static methods (again)

    Tomas Mikula, Oct 19, 2009, in forum: Java
    Replies:
    67
    Views:
    2,008
    Tomas Mikula
    Nov 4, 2009
Loading...

Share This Page