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

Static const class members, definition and initialization

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 non-standard C++ or if the
problem lies elsewhere.

To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const members are initialized
within the source (.cpp) file instead of within the class declaration.
Non-const members however seem to work fine, and the member seems to be
fine when accessed within the DLL.

I've googled on "static const" and searched the cygwin mailing lists,
but could not find any related discussions.

I've also scoured a draft C++ standard that I found on the web [ISO C++]
and though I found the section that describes initialization of static
members, it did not seem to mention any difference caused by const.

The main question that I want to find answer to is:

Must static const members be initialized within the class
declaration?, or can they also be initialized within the class
implementation?

Followed is an example:

A.dll
A.h:
class A
{
public:
static const int cmember;
static int member;
}

A.cpp:
const int A::cmember = 10;
int A::member = 15;

main.cpp
#include <A.h>
#include <cstdio>

fprintf( stdout, "Value of A::cmember %i\n", A::cmember );
fprintf( stdout, "Value of A::member %i\n", A::member );

Below is the output from a similar program - compiled and run under
cygwin - that has 3 classes A, B and C, and the main function. Class A
and B are each within their own DLL. Class C is linked at compile time
with main to produce the executable. BTW The same code compiled (as .so
libraries) and run under Linux runs as (I) expected.

A's Members via B
cmember: 1359750655
member: 15
A's Members straight from A
cmember: 1098655231
member: 15
A's Members via A's static methods
cmember: 10
member: 15
A's Members via A's instance methods
cmember: 10
member: 15
C's Members straight from C
cmember: 20
member: 30
A's Members via B (2nd try)
cmember: 1359750655
member: 15
A's Members straight from A (2nd try)
cmember: 1098655231
member: 15

Thank you for time,
Cheers,
Daniel Bradley

[ISO C++]
Initialization of non-local objects (section 3.6.2)
Working Paper for Draft Proposed International Standard for Information
Systems - Programming Language C++
http://www.open-std.org/jtc1/sc22/open/n2356/
Jul 22 '05 #1
3 3572
"DanielBradley" <d.*******@imb.uq.edu.au> wrote...
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 non-standard C++ or if the
problem lies elsewhere.

To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const members are initialized
within the source (.cpp) file instead of within the class declaration.
Non-const members however seem to work fine, and the member seems to be
fine when accessed within the DLL.
Just to let you know, DLLs are not specified by the language definition,
so anything particular to them is not on topic of this newsgroup. Just
to let you know...
I've googled on "static const" and searched the cygwin mailing lists,
but could not find any related discussions.

I've also scoured a draft C++ standard that I found on the web [ISO C++]
and though I found the section that describes initialization of static
members, it did not seem to mention any difference caused by const.
There shouldn't be any difference.
The main question that I want to find answer to is:

Must static const members be initialized within the class
declaration?
Must? No. Can? Yes, but only if they are of integral type. If
they are not integral (enum, int, char), they must NOT be initailised
in the class definition (not declaration). In addition to that, even
if a const _is_ initialised in the class definition, it still has to
be defined _outside_ the class definition if it's used in the program
(outside the class definition, that is).
, or can they also be initialized within the class
implementation?
There can be only one _initialisation_. However, if you remember
that _any_ static data members need to be defined and initialised
_outside_ the class definition, you cannot go wrong.

Followed is an example:
Apparently, you typed it in right here instead of posting real
and compileable code. Try to avoid doing that in the future.

A.dll
A.h:
class A
{
public:
static const int cmember;
static int member;
}
;

A.cpp:
#include <A.h>
const int A::cmember = 10;
int A::member = 15;

main.cpp
#include <A.h>
#include <cstdio>

fprintf( stdout, "Value of A::cmember %i\n", A::cmember );
fprintf( stdout, "Value of A::member %i\n", A::member );
This is not a valid C++ program.

Below is the output from a similar program - compiled and run under
cygwin - that has 3 classes A, B and C, and the main function. Class A
and B are each within their own DLL. Class C is linked at compile time
with main to produce the executable. BTW The same code compiled (as .so
libraries) and run under Linux runs as (I) expected.

A's Members via B
What does "via B" mean?
cmember: 1359750655
member: 15
A's Members straight from A
cmember: 1098655231
member: 15
A's Members via A's static methods
cmember: 10
member: 15
A's Members via A's instance methods
cmember: 10
member: 15
C's Members straight from C
cmember: 20
member: 30
A's Members via B (2nd try)
cmember: 1359750655
member: 15
A's Members straight from A (2nd try)
cmember: 1098655231
member: 15


So, you have some kind of a problem of either _initialisation_ or
_non-shared_ data segments. You should really seek advice from
a compiler newsgroup. See the list of suggested newsgroups in the
'Welcome' message posted here weekly.

Victor
Jul 22 '05 #2
Victor Bazarov wrote:

Victor, thank you for your response. It would seem that I am writing
standards compliant C++ and should look for more answers elsewhere.

I think the next thing to do is to test the code using a different
Windows compiler to see if the issue is specific to cygwin or not. As it
seems the C++ is correct I'll also post a message to the cygwin mailing
list hopefully they will be interested regardless.

I'll post a follow up here when I find the problem.

I have answered your questions below.

Thanks,
Daniel.
"DanielBradley" <d.*******@imb.uq.edu.au> wrote...
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 non-standard C++ or if the
problem lies elsewhere.

To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const members are initialized
within the source (.cpp) file instead of within the class declaration.
Non-const members however seem to work fine, and the member seems to be
fine when accessed within the DLL.

Just to let you know, DLLs are not specified by the language definition,
so anything particular to them is not on topic of this newsgroup. Just
to let you know...

I've googled on "static const" and searched the cygwin mailing lists,
but could not find any related discussions.

I've also scoured a draft C++ standard that I found on the web [ISO C++]
and though I found the section that describes initialization of static
members, it did not seem to mention any difference caused by const.

There shouldn't be any difference.

The main question that I want to find answer to is:

Must static const members be initialized within the class
declaration?

Must? No. Can? Yes, but only if they are of integral type. If
they are not integral (enum, int, char), they must NOT be initailised
in the class definition (not declaration). In addition to that, even
if a const _is_ initialised in the class definition, it still has to
be defined _outside_ the class definition if it's used in the program
(outside the class definition, that is).

, or can they also be initialized within the class
implementation?

There can be only one _initialisation_. However, if you remember
that _any_ static data members need to be defined and initialised
_outside_ the class definition, you cannot go wrong.

Followed is an example:

Apparently, you typed it in right here instead of posting real
and compileable code. Try to avoid doing that in the future.


Hmmm, sorry about that, was actually type copied from my code, just
forgot the important parts of main :) Should have copied and pasted.
A.dll
A.h:
class A
{
public:
static const int cmember;
static int member;
}

;

A.cpp:

#include <A.h>
const int A::cmember = 10;
int A::member = 15;

main.cpp
#include <A.h>
#include <cstdio>

fprintf( stdout, "Value of A::cmember %i\n", A::cmember );
fprintf( stdout, "Value of A::member %i\n", A::member );

This is not a valid C++ program.

Below is the output from a similar program - compiled and run under
cygwin - that has 3 classes A, B and C, and the main function. Class A
and B are each within their own DLL. Class C is linked at compile time
with main to produce the executable. BTW The same code compiled (as .so
libraries) and run under Linux runs as (I) expected.

A's Members via B

What does "via B" mean?


Accessed from A via a instance method in B.

#include "B.h"
B.cpp________________________________

#include <A.h>
int
B::getConstMemberOfA()
{
return A::cmember;
}
int
B::getMemberOfA()
{
return A::member;
}
____________________________________
cmember: 1359750655
member: 15
A's Members straight from A
cmember: 1098655231
member: 15
A's Members via A's static methods
cmember: 10
member: 15
A's Members via A's instance methods
cmember: 10
member: 15
C's Members straight from C
cmember: 20
member: 30
A's Members via B (2nd try)
cmember: 1359750655
member: 15
A's Members straight from A (2nd try)
cmember: 1098655231
member: 15

So, you have some kind of a problem of either _initialisation_ or
_non-shared_ data segments. You should really seek advice from
a compiler newsgroup. See the list of suggested newsgroups in the
'Welcome' message posted here weekly.

Victor


Jul 22 '05 #3
DanielBradley <d.*******@imb.uq.edu.au> wrote in message news:<40**************@imb.uq.edu.au>...
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 non-standard C++ or if the
problem lies elsewhere.

To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const members are initialized
within the source (.cpp) file instead of within the class declaration.
Non-const members however seem to work fine, and the member seems to be
fine when accessed within the DLL.

I've googled on "static const" and searched the cygwin mailing lists,
but could not find any related discussions.

I've also scoured a draft C++ standard that I found on the web [ISO C++]
and though I found the section that describes initialization of static
members, it did not seem to mention any difference caused by const.

The main question that I want to find answer to is:

Must static const members be initialized within the class
declaration?,
No, in fact they cannot in general be initialized here, because then
you would run the risk of having multiple instances of the same
variable at link-time.
or can they also be initialized within the class
implementation?


This is the usual way of doing it, but there are certain restrictions,
as detailed below.

From reading your example, I think there is probably more to your
example than you are stating. Because you are dealing with multiple
translation/compilation units, I suspect you are having an order of
initialization problem with your static variables. I recently
participated in a fairly extensive discussion on exactly this topic in
comp.lang.c++.moderated which you may find useful:
http://groups.google.com/groups?hl=e...ing.google.com

In any case, the bottom line is that there are absolutely NO
guarantees about order of initialization of static data (const or
otherwise) across compilation units. All that you can know for sure
(according to the Standard) is that static member data is initialized
EITHER:

1) when program flow enters main

OR in the specific case that initialization is deferred until after
main is entered

2) before it is used for the first time.

AND that within a translation unit, static members are initialized in
the order that their initialization statements are encountered.

Note that the fact that your static data members are const is
immaterial .. what matters is whether or not they are statically
(compile-time) or dynamically (run-time) initialized. Clearly the
second must be true in your case or else you would not be having these
problems.

So, you should avoid creating dependencies between static data members
in different compilation units. OTOH, if you have no such dependecies
in your code and the test cases are still producing screwy results,
then I suspect your compiler may be at fault. Note that there may be
some sort of "pragma hack" (i.e. non-Standard) provided by compiler
vendors to deal with this sort of issue.

Finally, the fact that you are using .DLL's (which I have little
experience with) may also complicate matters. There is another thread
currently under discussion on comp.lang.c++.moderated that you may
find useful, although it is not directly related to static member
data, there is a lot of information about .DLL's and how objects are
distributed across them.
http://groups.google.com/groups?q=g:...ing.google.com

HTH, Dave Moore
[example snipped]
Jul 22 '05 #4

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

Similar topics

4
by: Rakesh Sinha | last post by:
I am having this code here. static const float PI = 3.14159; static const float INC = 0.4f * PI; When I compile my program, I get the following error, error: `MyClass::PI' cannot appear...
12
by: Philip Potter | last post by:
I'm reading the comp.lang.c++ faq and, though it is incredibly clear and lucid in many points, I am completely confused by question 29.6: ...
10
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
5
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.