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

H

Hubert Fritz

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
 
I

Ian Collins

Hubert said:
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?
 
H

Hubert Fritz

Ian said:
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
 
I

Ian Collins

Hubert said:
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;
}
 
H

Hubert Fritz

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
 
I

Ian Collins

Hubert Fritz wrote:

Please try not to top post.
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++!
 
G

Greg Herlihy

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top