Possible for C++ class to force derived class to contain static datamember?

Discussion in 'C++' started by Hubert Fritz, Jan 28, 2007.

  1. Hubert Fritz

    Hubert Fritz Guest

    Hello,

    I fear I want to have something which is not possible in C++.

    How is it possible to define a base class so that the derived
    class is forced to contain a static member variable, which can
    be used by static member functions of the base class?
    Something like
    virtual static XClass* pXClass;
    /* pXClass shall be pure virtual, so not static in base class, but
    static in derived class */
    The static class related part of the derivation could so provide
    manager functionality for the object instances of the class.

    The only solution I currently see is to have a base class
    containing
    XClass* pXClass; /* non static */
    and related member functions.
    Derive a singleton class from this, so I would have some "static"
    effect. And have a separate class XClass chained by the derived class.


    Best regards,
    Hubert
     
    Hubert Fritz, Jan 28, 2007
    #1
    1. Advertising

  2. Hubert Fritz

    Ian Collins Guest

    Re: Possible for C++ class to force derived class to contain staticdata member?

    Hubert Fritz wrote:
    > Hello,
    >
    > I fear I want to have something which is not possible in C++.
    >
    > How is it possible to define a base class so that the derived
    > class is forced to contain a static member variable, which can
    > be used by static member functions of the base class?


    You might be able to do something with templates, but what problem are
    you attempting to solve?

    --
    Ian Collins.
     
    Ian Collins, Jan 28, 2007
    #2
    1. Advertising

  3. Hubert Fritz

    Hubert Fritz Guest

    Re: Possible for C++ class to force derived class to contain staticdata member?

    Ian Collins wrote:
    > Hubert Fritz wrote:
    >> Hello,
    >>
    >> I fear I want to have something which is not possible in C++.
    >>
    >> How is it possible to define a base class so that the derived
    >> class is forced to contain a static member variable, which can
    >> be used by static member functions of the base class?

    >
    > You might be able to do something with templates, but what problem are
    > you attempting to solve?
    >


    My problem is more a theoretical one - to understand a) the limitations
    of the language or b) my limitated understanding of
    the language.

    Given that you want to manage objects of a class, e.g. create
    them, find them again by several keys, delete them. So you
    might have an anchor, and chained the objects in a list.
    Using a single class to implement this concept, you can have
    the anchor static - it will belong to the class. The link
    field in the class will be non-static and belongs to the
    objects. Member functions manipulating the linked list
    would be static for being used independently of particular
    objects. Let us name this class ClassA.

    Now imagine to generalise this and try to find a base class
    ClassB gathering the algorithms and derive one, or several
    classes with functionality such as ClassA, we may call them
    ClassD1, ... ClassDn.
    The static member (anchor) of ClassA is obviously needed in
    the derived classes. Algorithms shall be inherited from ClassB.

    For me it is not clear if there is a way to provide
    or force the definition of the static member in ClassD1
    by ClassB and use it from static member functions in
    ClassB.

    Hubert
     
    Hubert Fritz, Jan 28, 2007
    #3
  4. Hubert Fritz

    Ian Collins Guest

    Re: Possible for C++ class to force derived class to contain staticdata member?

    Hubert Fritz wrote:
    >
    > My problem is more a theoretical one - to understand a) the limitations
    > of the language or b) my limitated understanding of
    > the language.
    >
    > Given that you want to manage objects of a class, e.g. create
    > them, find them again by several keys, delete them. So you
    > might have an anchor, and chained the objects in a list.
    > Using a single class to implement this concept, you can have
    > the anchor static - it will belong to the class. The link
    > field in the class will be non-static and belongs to the
    > objects. Member functions manipulating the linked list
    > would be static for being used independently of particular
    > objects. Let us name this class ClassA.
    >
    > Now imagine to generalise this and try to find a base class
    > ClassB gathering the algorithms and derive one, or several
    > classes with functionality such as ClassA, we may call them
    > ClassD1, ... ClassDn.
    > The static member (anchor) of ClassA is obviously needed in
    > the derived classes. Algorithms shall be inherited from ClassB.
    >
    > For me it is not clear if there is a way to provide
    > or force the definition of the static member in ClassD1
    > by ClassB and use it from static member functions in
    > ClassB.
    >

    I'm still not 100% clear what you are trying to achieve, but would
    something like this do what you want?

    #include <iostream>

    template <typename Derived>
    struct B
    {
    static int doSomething()
    {
    return Derived::staticMember;
    }
    };

    struct D : B<D>
    {
    static const int staticMember = 42;
    };

    int main()
    {
    std::cout << D::doSomething() << std::endl;
    }

    --
    Ian Collins.
     
    Ian Collins, Jan 28, 2007
    #4
  5. Hubert Fritz

    Hubert Fritz Guest

    Re: Possible for C++ class to force derived class to contain staticdata member?

    Hello Ian,

    I have to think about and try out the
    reachability of staticMember by B.
    The static member in D seems not to be
    enforced by struct B. But this is no
    severe problem.
    Somehow strange that the base class is
    dependent from the derived one.

    Hubert Fritz


    Ian Collins wrote:

    > I'm still not 100% clear what you are trying to achieve, but would
    > something like this do what you want?
    >
    > #include <iostream>
    >
    > template <typename Derived>
    > struct B
    > {
    > static int doSomething()
    > {
    > return Derived::staticMember;
    > }
    > };
    >
    > struct D : B<D>
    > {
    > static const int staticMember = 42;
    > };
    >
    > int main()
    > {
    > std::cout << D::doSomething() << std::endl;
    > }
    >
     
    Hubert Fritz, Jan 28, 2007
    #5
  6. Hubert Fritz

    Ian Collins Guest

    Re: Possible for C++ class to force derived class to contain staticdata member?

    Hubert Fritz wrote:

    Please try not to top post.

    >
    > Ian Collins wrote:
    >
    >>I'm still not 100% clear what you are trying to achieve, but would
    >>something like this do what you want?
    >>
    >>#include <iostream>
    >>
    >>template <typename Derived>
    >>struct B
    >>{
    >> static int doSomething()
    >> {
    >> return Derived::staticMember;
    >> }
    >>};
    >>
    >>struct D : B<D>
    >>{
    >> static const int staticMember = 42;
    >>};
    >>
    >>int main()
    >>{
    >> std::cout << D::doSomething() << std::endl;
    >>}
    >>

    > Hello Ian,
    >
    > I have to think about and try out the
    > reachability of staticMember by B.


    Either make it public, or

    class D : public B<D>
    {
    friend class B<D>;
    static const int staticMember = 42;
    };

    > The static member in D seems not to be
    > enforced by struct B. But this is no
    > severe problem.


    It is if anything tries to call doSomething().

    > Somehow strange that the base class is
    > dependent from the derived one.
    >

    Welcome to the world of C++!

    --
    Ian Collins.
     
    Ian Collins, Jan 28, 2007
    #6
  7. Hubert Fritz

    Greg Herlihy Guest

    Re: Possible for C++ class to force derived class to contain static data member?

    On Jan 28, 12:35 pm, Hubert Fritz <> wrote:
    > Hello,
    >
    > I fear I want to have something which is not possible in C++.
    >
    > How is it possible to define a base class so that the derived
    > class is forced to contain a static member variable, which can
    > be used by static member functions of the base class?


    The answer is simple: have the base class declare the static data
    member itself - thereby ensuring that the static data member exists
    and is accessible from the derived class.

    After all, what difference does it make whether the base class or its
    derived class is the one to declare the static data member? The
    overhead in either case is exactly the same.

    Greg
     
    Greg Herlihy, Jan 28, 2007
    #7
    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. john sun
    Replies:
    3
    Views:
    360
    Martijn Lievaart
    Feb 6, 2004
  2. Replies:
    1
    Views:
    407
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    398
    Victor Bazarov
    May 23, 2007
  4. vivek_12315

    Threads having datamember as HashMap

    vivek_12315, Nov 23, 2010, in forum: Java
    Replies:
    2
    Views:
    275
  5. Keith Patrick

    Adding DataMember support to SqlDataSource

    Keith Patrick, Dec 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    180
    Keith Patrick
    Dec 25, 2005
Loading...

Share This Page