473,513 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Q] const declaration in .h file

I am attempting to figure out why this isn't working, because it seems
like it should be...

First I declare things similar to this:

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { keyA };
};

When I look at, astructarray[0].a, I get back a value of 0.

When I change this to:

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { { 10, 20 } };
};

and look at astructarray[0].a, I get back a value of 10.
Why should things not work in the first case? Or should they?

Jul 22 '05 #1
5 1387
salvor wrote:
I am attempting to figure out why this isn't working, because it seems
like it should be...

First I declare things similar to this:

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { keyA };
};

When I look at, astructarray[0].a, I get back a value of 0.

When I change this to:

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { { 10, 20 } };
};

and look at astructarray[0].a, I get back a value of 10.
Why should things not work in the first case? Or should they?


You may be running into "static object initialisation order fiasco".

When I run this:
----------------------
#include <iostream>
using namespace std;

typedef unsigned UInt32;

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };
const astruct astructarray[] = { keyA };
};

int main() {
cout << myns::astructarray[0].a << endl;
}
---------------------
I get '10'. So, the question is, where do you "loot at astructarray[0].a"
in your first reported case, when you get 0?

Victor
Jul 22 '05 #2
Victor Bazarov <v.********@comAcast.net> wrote:
salvor wrote:
I am attempting to figure out why this isn't working, because it seems
like it should be...

First I declare things similar to this:
CASE #1

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { keyA };
};

When I look at, astructarray[0].a, I get back a value of 0.

When I change this to:
CASE #2

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { { 10, 20 } };
};

and look at astructarray[0].a, I get back a value of 10.
Why should things not work in the first case? Or should they?
You may be running into "static object initialisation order fiasco".


This is what I figured might be happening.

Is there any guaranteed way to get out of this mess without moving
totally to the second case?

I liked the first case since it is useful to be able to use keyA itself
and be able to place keyA in an array. This way, if I needed to make a
change to keyA, I would not have to remember and correctly make the
change to the array as well.

I attempted to check the FAQ (http://www.parashift.com/c++-faq-lite/),
but it is unclear which major heading this topic would be covered under.
When I run this:
----------------------
#include <iostream>
using namespace std;

typedef unsigned UInt32;

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };
const astruct astructarray[] = { keyA };
};

int main() {
cout << myns::astructarray[0].a << endl;
}
---------------------
I get '10'. So, the question is, where do you "loot at astructarray[0].a"
in your first reported case, when you get 0?


Well, I am not sure how one "loot's", but I look at astructarray[0].a
well after my event driven application is underway and after I select an
item from a menu which has the code to look at astructarray[0].a.

Jul 22 '05 #3
Eric wrote:
Victor Bazarov <v.********@comAcast.net> wrote:
salvor wrote:
If you are the original poster, you might consider keeping the same
username, just for consistency. Unless these are your tow different
personalities posting, then I'd rather not comment.
I am attempting to figure out why this isn't working, because it seems
like it should be...

First I declare things similar to this:


CASE #1

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { keyA };
};

When I look at, astructarray[0].a, I get back a value of 0.

When I change this to:

CASE #2

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { { 10, 20 } };
};

and look at astructarray[0].a, I get back a value of 10.
Why should things not work in the first case? Or should they?


You may be running into "static object initialisation order fiasco".

This is what I figured might be happening.

Is there any guaranteed way to get out of this mess without moving
totally to the second case?

I liked the first case since it is useful to be able to use keyA itself
and be able to place keyA in an array. This way, if I needed to make a
change to keyA, I would not have to remember and correctly make the
change to the array as well.


What you did is initialised an element of the array with the same
value as in 'keyA'. Since an object with a namespace scope has static
storage duration, it is possible that a function in another module
will be called before 'astructarray' is initialised.

I attempted to check the FAQ (http://www.parashift.com/c++-faq-lite/),
but it is unclear which major heading this topic would be covered under.
FAQ # 10.15 talks about it. If you just searched by the word "fiasco",
you'd have found it.

When I run this:
----------------------
#include <iostream>
using namespace std;

typedef unsigned UInt32;

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };
const astruct astructarray[] = { keyA };
};

int main() {
cout << myns::astructarray[0].a << endl;
}
---------------------
I get '10'. So, the question is, where do you "loot at astructarray[0].a"
in your first reported case, when you get 0?

Well, I am not sure how one "loot's", but I look at astructarray[0].a
well after my event driven application is underway and after I select an
item from a menu which has the code to look at astructarray[0].a.


I don't know what "event driven application" is in terms of C++ and
how one "selects an item from a menu" (again in C++ terms). These
concepts are unknown as far as C++ _language_ is concerned. I showed
to you what happens in a C++ program when I print out the value of
the array element data member in question. How you _look_ at it,
again, is unknown. Perhaps if you specified what "looking" meant,
we would be on the same page.

Reduce the code you have to the bare minimum that exhibits the "error"
you originally posted about. Then post it here. IOW, follow the
recommendations of FAQ 5.8.

Victor
Jul 22 '05 #4
On Thu, 14 Oct 2004 12:49:18 -0400, sN************@verizon.net
(salvor) wrote:
I am attempting to figure out why this isn't working, because it seems
like it should be...

First I declare things similar to this:

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { keyA };
};

When I look at, astructarray[0].a, I get back a value of 0.

When I change this to:

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { { 10, 20 } };
};

and look at astructarray[0].a, I get back a value of 10.
Why should things not work in the first case? Or should they?


The problem is that "keyA" isn't a constant expression, so
astructarray isn't initialized at compile time, but at runtime. Hence
you run into the static initialization order fiasco. If you do it in
the second way, you are initializing astructarray with constant
expressions, so it is initialized at compile time, and there can't be
any problem.

You could put the 10 and 20 into separate variables. e.g.

const int a1 = 10;
const int a2 = 20; //note "a1" and "a2" are constant expressions
const astruct keyA = { a1, a2 };
const astruct astructarray[] = { {a1, a2} };

Unfortunately, you can only make constant expressions out of a few
things, like integral literals and the addresses of global objects.
However, const integral variables initialized with constant
expressions are also constant expressions (like "a1" above).

Tom
Jul 22 '05 #5
Eric wrote:
Victor Bazarov <v.********@comAcast.net> wrote:

salvor wrote:
I am attempting to figure out why this isn't working, because it seems
like it should be...

First I declare things similar to this:

CASE #1

struct astruct
{
UInt32 a;
UInt32 b;
};

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { keyA };
};

When I look at, astructarray[0].a, I get back a value of 0.

When I change this to:

CASE #2

namespace myns
{
const astruct keyA = { 10, 20 };

const astruct astructarray[] = { { 10, 20 } };
};

and look at astructarray[0].a, I get back a value of 10.
Why should things not work in the first case? Or should they?


You may be running into "static object initialisation order fiasco".

This is what I figured might be happening.

Is there any guaranteed way to get out of this mess without moving
totally to the second case?

I liked the first case since it is useful to be able to use keyA itself
and be able to place keyA in an array. This way, if I needed to make a
change to keyA, I would not have to remember and correctly make the
change to the array as well.


how about

namespace myns
{
const astruct astructarray[] = { { 10, 20 } };
const astruct& keyA = astructarray[0];
}
Jul 22 '05 #6

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

Similar topics

5
13263
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is...
8
2574
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
3
3582
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...
5
1694
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
12
16126
by: Riley DeWiley | last post by:
I am looking for a graceful way to declare a string const that is to be visible across many files. If I do this: //----hdr.h const char * sFoo = "foo"; //file.cpp
9
5107
by: C. J. Clegg | last post by:
When you say "const int FOO = 0" (as the commonly-recommended C++ alternative to "#define FOO 0"), isn't that declaration globally visible? I have "const int FOO = 0;" in one source file and...
3
2661
by: Alexander Hans | last post by:
Hi, what's the usual way in C++ to define a static constant class member? Consider the following example: class TestClass { public: static const double a = 12.5; static const double b = 2...
7
2064
by: W Marsh | last post by:
Hi, Could anybody tell me wh the parameter "T val" is not marked const in this Stroustrup code, considering that val is not modified and not non- const methods called? template<class C,...
10
5927
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
15
7839
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?
0
7267
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
7391
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
7553
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...
1
7120
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...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1609
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 ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
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...

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.