acces a static variable through base class

A

Anthony

Hi,
Can anyone help me out here?

I need a construction where a static variable in a derived class is
accessible through its base class.
The base class is part of a framework.
The variable in the derived class must be static,
because the framework must access this variable when instantiation has not
taken place yet.
After instantiation other parts of the framework must access this variable
through the base class.

Is there a design pattern or neat c++ construction for this problem?

Thanks in advance,
Anthony Lansbergen
 
A

Attila Feher

Anthony said:
Hi,
Can anyone help me out here?

I need a construction where a static variable in a derived class is
accessible through its base class.
The base class is part of a framework.
The variable in the derived class must be static,
because the framework must access this variable when instantiation
has not taken place yet.
After instantiation other parts of the framework must access this
variable through the base class.

Is there a design pattern or neat c++ construction for this problem?

Give some more context. In general: it cannot be done. Initialization
order of statics across translation units is indefined in C++.
 
F

Frank Schmitt

Anthony said:
Hi,
Can anyone help me out here?

I need a construction where a static variable in a derived class is
accessible through its base class.
The base class is part of a framework.
The variable in the derived class must be static,
because the framework must access this variable when instantiation has not
taken place yet.
After instantiation other parts of the framework must access this variable
through the base class.

This sounds like a design flaw to me - a base class shouldn't need to know
about its descendants.
Is there a design pattern or neat c++ construction for this problem?

You could use a static member function in Derived returning the value.

HTH & kind regards
frank
 
A

Anthony

Attila Feher said:
Give some more context. In general: it cannot be done. Initialization
order of statics across translation units is indefined in C++.

Hi,
Here some more context for my problem:

I have a Class BaseContainer and a class BaseComponent in my framework.
Concrete containers and components are created by deriving from the base
classes.
The components must run in the containers execution thread.
The concrete containers are static defined in the application.
Thus to bind a component to a container I want every component to hold a
static reference to it's container.
This static reference must be accessible through its base class
"BaseComponent".
This way I can generalize (in the framework) all components as
BaseComponents and still reach their (base) containers.
The reason that this Container pointer must be static, is that the framework
also uses a templated builder class (parameterised class) .
I use this builder mechanism to create an component a-synchrone in a
multithreaded system.

Thanks in advance,

Anthony Lansbergen
 
A

Attila Feher

Anthony wrote:
[SNIP]
Here some more context for my problem:

I have a Class BaseContainer and a class BaseComponent in my
framework. Concrete containers and components are created by deriving
from the base classes.
The components must run in the containers execution thread.
The concrete containers are static defined in the application.
Thus to bind a component to a container I want every component to
hold a static reference to it's container.
This static reference must be accessible through its base class
"BaseComponent".
This way I can generalize (in the framework) all components as
BaseComponents and still reach their (base) containers.
The reason that this Container pointer must be static, is that the
framework also uses a templated builder class (parameterised class) .
I use this builder mechanism to create an component a-synchrone in a
multithreaded system.

I must be dumb, but I still don't get it. You cannot make a static, which
is guaranteed to be initialized before the framework tries to access it.
Unless it is a compile time contant.

Does the framework access you object, or directly that pointer?

What does "The concrete containers are static defined in the application."
means?

etc.
 
A

Anthony

I must be dumb, but I still don't get it. You cannot make a static, which
is guaranteed to be initialized before the framework tries to access it.
Unless it is a compile time contant.

Does the framework access you object, or directly that pointer?

What does "The concrete containers are static defined in the application."
means?

etc.

Hi, thanks for your input,

I think the solution I have now is to complex to explain and that already
shows that I have got a poor design.
So the problem is more a design question than an implementation solution I
'm looking for.
Thanks for your time,
I will post a new question to this group which will be more a design
question.

Greetings,
Anthony Lansbergen
 
M

Michel de Becdelièvre

For problems of static initialisation in a multithread environment, look
at how it is done for cout and cin (if your instantiation is done a program
startup).
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top