473,672 Members | 3,797 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 1392
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::astructar ray[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.********@com Acast.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::astructar ray[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.********@com Acast.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::astructar ray[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.********@com Acast.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
13276
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 instantiated, but is visible to all of the methods of only this class ? const unsigned short ConstantVectorWidth = 10; const float ConstantVector = {5,10,15,20,25,30,35,40,45,50}; Does this syntax look correct?
8
2587
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
3598
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 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...
5
1705
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
16168
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
5113
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 "extern int FOO;" in another source file (also tried "extern const int FOO;") and at link time, I get undefined references to FOO. Removed the "const" from the FOO declaration and everything worked fine.
3
2664
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 * a;
7
2079
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, class Tint count(const C&v, T val) { typename C::const_iterator i = find(v.begin(), v.end(), val); int n = 0;
10
5959
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 is unused, I know that it does not take up storage in the
15
7855
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
8503
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
8844
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
8642
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
7472
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...
1
6254
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4239
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2092
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1836
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.