473,396 Members | 2,052 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,396 software developers and data experts.

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

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
Jul 23 '05 #1
1 1154

Eric Lilja wrote:
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

Jul 23 '05 #2

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

Similar topics

5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
7
by: cFleury | last post by:
I have an ASP.NET web form that is retaining values over different stations on the network, how can I stop this behavior ?. I didn’t do anything special to cause it. Thanks C.Fleury
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
7
by: sparks | last post by:
I am working on a database that has a lot of calculated values on the forms. These were never put into the tables. But were tied to unbound fields on the forms. Now 8000 records later they want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.