How should I set the values of these static data members?

E

Eric Lilja

Hello, I have a class in my program and there may be several objects around
of the class at any given time. The class has seven data members that
qualify for static linkage, but only one of them will have it value known
during compile time. The other six data members are basically pointer types
and can only receive meaningful values during runtime.
Right now I am using the constructor the give these static members their
values (they're initialized to NULL) but it feels wrong because that means
that for each object I create, I pass the same data for six variables. (I
also noticed I couldn't assign values in the initializer list, had to be in
the body.) So in the body of the constructor I have:
if(!m_static_data_member)
m_static_data_member = some_variable_passed_to_the_ctor;
* 6...

It doesn't feel elegant...what should I do?

/ Eric
 
R

rajkumar

Eric said:
Hello, I have a class in my program and there may be several objects around
of the class at any given time. The class has seven data members that
qualify for static linkage, but only one of them will have it value known
during compile time. The other six data members are basically pointer types
and can only receive meaningful values during runtime.
Right now I am using the constructor the give these static members their
values (they're initialized to NULL) but it feels wrong because that means
that for each object I create, I pass the same data for six variables. (I
also noticed I couldn't assign values in the initializer list, had to be in
the body.) So in the body of the constructor I have:
if(!m_static_data_member)
m_static_data_member = some_variable_passed_to_the_ctor;
* 6...

It doesn't feel elegant...what should I do?

/ Eric

1) way
int main()
{
InitializeEverything();

/* do rest of the stuff */
}

2) Have a special constructor for your class and do the initialization
only in that constructor. And some where in your cpp you can do

TheObjectInQuestion a(special constructors arguments);

Read up on the faq about static initialization order fiasco

Thanks
Raj
 

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,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top