473,326 Members | 2,196 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,326 software developers and data experts.

Static member in a class

I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"

May 8 '07 #1
10 2618
stonny wrote:
I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"
The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That's why you should initialise static data members in
a .cpp file (and only in one .cpp file).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 8 '07 #2
* Victor Bazarov:
stonny wrote:
>I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"

The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That's why you should initialise static data members in
a .cpp file (and only in one .cpp file).
Initialization accompanies definition of a constant except in the case
when a declaration of a static constant in a class supplies the
initializer, in which case the definition (if any, and then outside the
class) is sans initializer.

Example:

struct S
{
static int const x = 42; // Not a definition, per §9.4.2/2.
};

If the address of S::x is used, or in the standard's terminology, if
S::x is "used", a definition is formally (but with current compilers not
necessarily in practice) required, outside the class:

int const S::x; // A definition, per §9.4.2/4.

Not directed at you, but at other readers: this construct is only
supported for integral types and/including enum types.

Directed at whoever thought up this crazy scheme: ugh.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 8 '07 #3
On May 8, 4:06 pm, stonny <zhangdexin2...@gmail.comwrote:
I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?
"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"
Static class data members aren't members of any particular class
instance. Unlike the other members of the class, the
declaration in the class is not a definition, so you need a
definition elsewhere.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 8 '07 #4
Victor Bazarov wrote:
stonny wrote:
>I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"

The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That's why you should initialise static data members in
a .cpp file (and only in one .cpp file).

V
This is such a pain, I agree. Some years ago, I started doing this:

// foo.h

class Phew [

static int barre;

...
}

#ifdef COMPILING_MAIN
int Phew::barre;
#endif

// end foo.h

and insisting that all modules containing main() define COMPILING_MAIN.
That way, all the static members get defined exactly once, and their
definitions are right there with the class declaration. One could use a
different #define name if one were making lots of shared libraries, but
this trick makes it much easier to keep things straight.

Cheers,

Phil Hobbs
May 9 '07 #5
On May 9, 5:27 am, Phil Hobbs <p...@SpamMeSenseless.pergamos.net>
wrote:
Victor Bazarov wrote:
stonny wrote:
I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?
"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.
"
The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That's why you should initialise static data members in
a .cpp file (and only in one .cpp file).
This is such a pain, I agree. Some years ago, I started doing this:
// foo.h
class Phew [
static int barre;
...
}
#ifdef COMPILING_MAIN
int Phew::barre;
#endif
// end foo.h
and insisting that all modules containing main() define COMPILING_MAIN.
And what happens if the module containing main() doesn't include
your header?

I'll admit that I don't see where it is such a big thing. I'll
just define the variable in whatever file I define the
constructor in.
That way, all the static members get defined exactly once, and their
definitions are right there with the class declaration.
But why do you want the definitions right there with the class
definition. You don't require that for functions; why is it a
problem to define static variables in a separate source file,
but not functions.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 9 '07 #6
James Kanze wrote:
On May 9, 5:27 am, Phil Hobbs <p...@SpamMeSenseless.pergamos.net>
wrote:
>Victor Bazarov wrote:
>>stonny wrote:
I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?
>>>"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.
"
>>The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That's why you should initialise static data members in
a .cpp file (and only in one .cpp file).
>This is such a pain, I agree. Some years ago, I started doing this:
>// foo.h
>class Phew [
static int barre;
...
}
>#ifdef COMPILING_MAIN
int Phew::barre;
#endif
>// end foo.h
>and insisting that all modules containing main() define COMPILING_MAIN.

And what happens if the module containing main() doesn't include
your header?

I'll admit that I don't see where it is such a big thing. I'll
just define the variable in whatever file I define the
constructor in.
> That way, all the static members get defined exactly once, and their
definitions are right there with the class declaration.

But why do you want the definitions right there with the class
definition. You don't require that for functions; why is it a
problem to define static variables in a separate source file,
but not functions.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
It's normally considerably easier to have the module containing main
include all the headers with static members. That's an easy rule to
remember.

Cheers,

Phil Hobbs
May 10 '07 #7
Phil Hobbs wrote:
James Kanze wrote:
>>
But why do you want the definitions right there with the class
definition. You don't require that for functions; why is it a
problem to define static variables in a separate source file,
but not functions.
*Please* don't quote signatures.
>
It's normally considerably easier to have the module containing main
include all the headers with static members. That's an easy rule to
remember.
What about static members of private classes in libraries? How would
you know (or even be able to) include the library's headers? What about
static member functions?

As James said, it's much easier to define them in the same module as the
class definition. After all, static members are no less members of the
class than its member function.

--
Ian Collins.
May 10 '07 #8
Phil Hobbs wrote:
It's normally considerably easier to have the module containing main
include all the headers with static members. That's an easy rule to
remember.
And how is the author of the module containing main supposed to
even know which headers exist with static members?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 10 '07 #9
Ian Collins wrote:
Phil Hobbs wrote:
>James Kanze wrote:
>>>
But why do you want the definitions right there with the class
definition. You don't require that for functions; why is it a
problem to define static variables in a separate source file,
but not functions.

*Please* don't quote signatures.
James' signature is not up to requirements. The two dashes _must_
be followed by a space to make the proper signature delimiter. So,
formally it's not a signature. <g>
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 10 '07 #10
Victor Bazarov wrote:
Ian Collins wrote:
Phil Hobbs wrote:
James Kanze wrote:

But why do you want the definitions right there with the class
definition. You don't require that for functions; why is it a
problem to define static variables in a separate source file,
but not functions.
*Please* don't quote signatures.

James' signature is not up to requirements. The two dashes must
be followed by a space to make the proper signature delimiter. So,
formally it's not a signature. <g>
Yeah, some of mentioned it to him before. I know he's using Google
Groups, and initially I thought that it might be eating the extra
space, but he said not and fixed it for a while, but soon was back to
the broken form. I don't know what the deal is.


Brian
May 10 '07 #11

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...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static 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...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
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?
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.