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

Iniitializing static const data?

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.

Probably it is because that we are in a sense calling a non static
function[ The constructor of std::string]. But any other soluction(?)
to me would sound inelegant.

But I see no rationale for not allowing

class Foo
{
static const float bar = 23;
};

Can anybody put any _common_sense in this non-sense.
--
Imanpreet Singh Arora
isingh AT acm DOT org
Jul 22 '05 #1
5 1677
Minti wrote:

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.

Probably it is because that we are in a sense calling a non static
function[ The constructor of std::string]. But any other soluction(?)
to me would sound inelegant.

But I see no rationale for not allowing

class Foo
{
static const float bar = 23;
};

Can anybody put any _common_sense in this non-sense.


What you are missing is that the above is a declaration, not a definition.
That is:
if you put a static member in a class, you only have declared it!
You still need a definition of that member somewhere and of course,
like anywhere else, you put the initialization to the definition.

Declaration: telling the compiler that somewhere, in a code snippet far far
away, something exists and what it looks like
Definition: reserves the actual memory for that something

So back to your example:

class Foo
{
static const std::string name; // Declaration
};

const std::string Foo::name = "MyName"; // Definition
Remember
There is the famous One-Definition-Rule. Which means:
you can have as many declarations in a program as you want (as long
as all of them coincide). But you can have only 1 defintion. Thus
typically the following is done. The declaration (enclosed in the class
declaration) goes into the header file, while the defintion (with the
initialization) goes into the source code file, which implements the
class functionality.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Minti wrote:
How do we initialize static const data in C++, I tried
class Foo
{
static const std::string name = "MyName";
};
Initialisation of static constants is allowed in the class definition
_only_ for integral types. Members of any other types (and arrays,
pointers, references) has to be defined/initialised at the namespace
level.


I don't see any reason as to why this must not work.
The only reason relevant in comp.lang.c++ is that the Standard does
not allow that.
Probably it is because that we are in a sense calling a non static
function[ The constructor of std::string]. But any other soluction(?)
to me would sound inelegant.
I am not sure I understand the last sentence here.

But I see no rationale for not allowing

class Foo
{
static const float bar = 23;
};

Can anybody put any _common_sense in this non-sense.


Yes. Somebody definitely can, but you have to ask in comp.std.c++.
The questions about _why_ things are in C++ the way they are belong
there, not here. Here we can tell you _how_ to do things, but not
usually _why_ (unless it's explicitly explained in the C++ language
specification). Language lawyers and Standard Committee members are
often found in comp.std.c++ where all "why" questions are discussed.

Victor
Jul 22 '05 #3
Minti posted:
class Foo
{
static const std::string name = "MyName";
};

Consider you have a project like so:

monkey.cpp
ape.cpp
cow.cpp
tiger.cpp
fonte.cpp

fonte.hpp
Here's fonte.hpp:

#ifndef INCLUDE_FONTE_HPP
#define INCLUDE_FONTE_HPP

class Foo
{
static const std::string name;
};

#endif

Now consider that this header file is included into all of the above CPP
files. If you actually define the variable "name" in the header file, then
each translation unit would have it's own separate variable. You don't want
this, you just want the one variable. How you do this is make "fonte.cpp"
and put the following in it:

#include "fonte.hpp"

const std::string Foo::name = "BlahBlah";

See what happens now: the actual variable is declared in a .CPP file, that's
where the defintions go. If another CPP file, another translation unit,
wants to access this variable, all it does is include "fonte.hpp"
-JKop
Jul 22 '05 #4
JKop <NU**@NULL.NULL> wrote in message news:<gv*****************@news.indigo.ie>...
Minti posted:
class Foo
{
static const std::string name = "MyName";
};

Consider you have a project like so:

monkey.cpp
ape.cpp
cow.cpp
tiger.cpp
fonte.cpp

fonte.hpp
Here's fonte.hpp:

#ifndef INCLUDE_FONTE_HPP
#define INCLUDE_FONTE_HPP

class Foo
{
static const std::string name;
};

#endif

Now consider that this header file is included into all of the above CPP
files. If you actually define the variable "name" in the header file, then
each translation unit would have it's own separate variable. You don't want
this, you just want the one variable. How you do this is make "fonte.cpp"
and put the following in it:

#include "fonte.hpp"

const std::string Foo::name = "BlahBlah";

See what happens now: the actual variable is declared in a .CPP file, that's
where the defintions go. If another CPP file, another translation unit,
wants to access this variable, all it does is include "fonte.hpp"

Hmm... Thanks for the answer but if we look at this whole thing with
an explanation that variables can only be declared in class
definitions then indeed we have a contradiction

class Foo
{
static const int foo = 23;
};

is allowed, the reason I believe is that it makes it easy to specify
the size of the arrays.

But this aan not be an explanation for making something incosistent.
--
Imanpreet Singh Arora "In order to paint 6 things are required:
i"kaur" #AT# acm #DOT# org spirit, rhythm, thought, scenery, pen
and ink"
mv kaur singh
Jul 22 '05 #5
Minti posted:

Hmm... Thanks for the answer but if we look at this whole thing with
an explanation that variables can only be declared in class
definitions then indeed we have a contradiction

class Foo
{
static const int foo = 23;
};

is allowed, the reason I believe is that it makes it easy to specify
the size of the arrays.

But this aan not be an explanation for making something incosistent.

My point is that no variable is created in your above code. Think of it as
an "extern" statement. It's just telling the compiler that such and object
exists, it's not saying that the variable is defined right here in this
translation unit!

The problem with your original code is that you were telling the compiler
that this object exists, but the when it came to the linker, the linker
couldn't find where the bloody thing was actually defined! That's why you
put:

const int Foo::foo = 23;

not 50 times, not 12 times, not twice, but once in one sole translation
unit. That way, when the linker sees loads of translation units saying that
this certain variable exists, it just goes through all of the translation
units looking for an actual definition, ie.:

const int Foo:foo = 23;

If it finds that more than once, then it's exactly the same as saying:
int main()
{
int a;

float a;

a = 23;
}
-JKop
Jul 22 '05 #6

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

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
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...
2
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data...
3
by: Thomas Matthews | last post by:
Hi, I've found with my compiler, that when a function contains const data in a function, that data is first copied to the stack. Given: /* Example 1 */ char Buffer; void My_Func(void)
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
5
by: Paul Smitton | last post by:
Hello, I would like to be able to store some constant data that is specific to each descendant class. This data would then be accessable by base class functions. However, I cannot find out how...
15
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?
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
5
by: greek_bill | last post by:
Hi, I'm trying to develop a system where I can register some data/ information about a class. For example // ClassInfo.h template <class T> struct ClassInfo { static const std::string ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.