473,385 Members | 1,312 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,385 software developers and data experts.

initialization of static const variables fails

Hi there, I've the following code (here a snippet).
Expand|Select|Wrap|Line Numbers
  1. /////////////////////////////////
  2. // *.h file
  3. /////////////////////////////////
  4. class ParameterTypeNames
  5. {
  6. public:
  7.     static const std::string FLOAT;
  8.     static const std::string INT;
  9.     // ...
  10. private:
  11.     inline ParameterTypeNames() {};
  12. };
  13.  
  14.  
  15. /////////////////////////////////
  16. // *.cpp file
  17. /////////////////////////////////
  18. const std::string ParameterTypeNames::FLOAT = "Float";
  19. const std::string ParameterTypeNames::INT   = "Int";
  20. // ....
  21.  
  22.  
  23. /////////////////////////////////
  24. // usage of the constants in a constructor of other class
  25. /////////////////////////////////
  26. // ...
  27. std::string s = ParameterTypeNames::FLOAT;
  28. // ...
In the last line (where the constant is actually being used), s holds an empty string, which I don't quite understand. Why is s not "Float"?? I thought that my initialization is correct.

Btw, I'm using VC 8.0 and it was in debug mode, if that is of any importance here.

I'd be happy about suggestions :-).

Greetings,
Felix
Apr 24 '07 #1
2 1595
Hi there, I've the following code (here a snippet).
[code]/////////////////////////////////
// *.h file
/////////////////////////////////
class ParameterTypeNames
{
public:
static const std::string FLOAT;
static const std::string INT;
// ...
private:
inline ParameterTypeNames() {};
};


/////////////////////////////////
// *.cpp file
/////////////////////////////////
const std::string ParameterTypeNames::FLOAT = "Float";
const std::string ParameterTypeNames::INT = "Int";
// ....


/////////////////////////////////
// usage of the constants in a constructor of other class
/////////////////////////////////
// ...
frnd i m not sure
but try this

class ParameterTypeNames
{
public:
static const std::string FLOAT="FLOAT";
static const std::string INT="INT";
// ...
private:
inline ParameterTypeNames() {};
Apr 24 '07 #2
hi, I have compiled your code and add a main() function:

int main(int argc, char** argv)
{
std::cout<<s<<std::endl;
return 0;
}

and the result I get is "Float", so it seems normal on my machine and I think your code is correct ...

btw, I'm using VS2005...
Apr 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
3
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...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
10
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
32
by: mdh | last post by:
Hi all, When I try and initialize this static array, as in: void itoa_recursively(int n, char *s){ static char *p = s; it fails, yet this:
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.