D
DanielBradley
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/
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/