473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Jan 28 '07 #1
6 3482
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.
Jan 28 '07 #2
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
Jan 28 '07 #3
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.
Jan 28 '07 #4

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;
}
Jan 28 '07 #5
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.
Jan 28 '07 #6


On Jan 28, 12:35 pm, Hubert Fritz <hubertfr...@arcor.dewrote:
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

Jan 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
3
by: Brad Navarro | last post by:
OK, I may be asking for the impossible, but I'll give it a shot: For a case like this: using System.Data.SqlClient; abstract class Base { private SqlConnection DBConnect; Base(string...
12
by: Bonj | last post by:
i'm trying to create a class that will contain all the features of instantiating a window and showing it. I've got the SetWindowLong / GetWindowLong pair to succesfully store the 'this' pointer in...
8
by: Tomás | last post by:
Given the following: static_cast<T>( expr ) This will evaluate to an l-value if T is a reference type -- otherwise it will evaluate to an r-value. The same goes for reinterpret_cast. ...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.