473,729 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3604
"DanielBrad ley" <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.
"DanielBrad ley" <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
implementatio n?

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::getConstMemb erOfA()
{
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++.m oderated 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++.m oderated 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
6192
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 in a constant-expression .
12
2903
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: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6 "Here is another even more common example: class Fred { public: ... private:
10
2651
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 static class member is that you cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file...
9
8891
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 *defined* "x"'s value to be 10, isn't above statement a
14
6021
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
5
5616
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
15
7864
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
8326
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
2
2482
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
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.