473,326 Members | 2,133 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.

Another static question

The following seems to work for g++. Is it legal though? I was thinking that
ALL non-const statics have to be initialized in the source files somewhere.

#include <iostream>

namespace nvps
{
static bool hi = false;

class Whatever
{
public:
static void printme();
};
}

void
nvps::Whatever::printme()
{
cout << hi << endl;
hi = true;
cout << hi << endl;
}

int main()
{
nvps::Whatever::printme();
}

~/personal/C++/>a.out
0
1


Jul 23 '05 #1
5 1259
cppsks wrote:
The following seems to work for g++. Is it legal though?
What makes you think it can be illegal?
I was thinking that
ALL non-const statics have to be initialized in the source files somewhere.
I am not sure what uninitialised statics you're alluding to.

#include <iostream>

namespace nvps
{
static bool hi = false;

class Whatever
{
public:
static void printme();
};
}

void
nvps::Whatever::printme()
{
cout << hi << endl;
hi = true;
cout << hi << endl;
}

int main()
{
nvps::Whatever::printme();
}

~/personal/C++/>a.out
0
1


V
Jul 23 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:6f*******************@newsread1.mlpsca01.us.t o.verio.net...
cppsks wrote:
The following seems to work for g++. Is it legal though?
What makes you think it can be illegal?
> I was thinking that
ALL non-const statics have to be initialized in the source files

somewhere.
I am not sure what uninitialised statics you're alluding to.
I thought that the statics have to be initialized in the source (.cc) file.
I guess that it may only pertain to class-level statics and not for the them
that are available/defined at the namespace level.


#include <iostream>

namespace nvps
{
static bool hi = false;

class Whatever
{
public:
static void printme();
};
}

void
nvps::Whatever::printme()
{
cout << hi << endl;
hi = true;
cout << hi << endl;
}

int main()
{
nvps::Whatever::printme();
}

~/personal/C++/>a.out
0
1


V

Jul 23 '05 #3
cppsks wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:6f*******************@newsread1.mlpsca01.us.t o.verio.net...
cppsks wrote:
The following seems to work for g++. Is it legal though?
What makes you think it can be illegal?
> I was thinking that

ALL non-const statics have to be initialized in the source files
somewhere.
I am not sure what uninitialised statics you're alluding to.

I thought that the statics have to be initialized in the source (.cc) file.


OK. I tried to make sense of that statement and it seems ambiguous to me.

Let me explain. "Statics have to be initialised in the source file". In
the context, when you say that you "thought that ...", you imply that the
statement is wrong. Or maybe you imply that whatever I wrote contradicts
this statement. Or perhaps that you now have information that makes you
change your views. So, which is it? If it's the former, then no, statics
don't have to be initialised [explicitly]. All statics are, in fact,
initialised upon program loading (or as part of program loading). So,
there is no need to initialise them. If the meaning of your "I thought
that..." is in fact that I said something to contradict this, what is it
you're referring to?
I guess that it may only pertain to class-level statics and not for the them
that are available/defined at the namespace level.
"Class-level statics" have to be initialised at the namespace level _iff_
they aren't initialised in the class definition. Also, consider that the
statement that puts a static class member in play is usually simply
a *declaration*, and as with any object, a *definition* is required if
the object is used elsewhere.

The last but not least note. In the source code you posted along with
your original question there are *no* uninitialised objects. Look at
it once again. That's partially why I said that I wasn't sure what
*uninitialised* statics you were alluding to.


#include <iostream>

namespace nvps
{
static bool hi = false;

class Whatever
{
public:
static void printme();
};
}

void
nvps::Whatever::printme()
{
cout << hi << endl;
hi = true;
cout << hi << endl;
}

int main()
{
nvps::Whatever::printme();
}

~/personal/C++/>a.out
0
1


V


V
Jul 23 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:QR*******************@newsread1.mlpsca01.us.t o.verio.net...
OK. I tried to make sense of that statement and it seems ambiguous to me.

Let me explain. "Statics have to be initialised in the source file". In
the context, when you say that you "thought that ...", you imply that the
statement is wrong. Or maybe you imply that whatever I wrote contradicts
this statement. Or perhaps that you now have information that makes you
change your views. So, which is it? If it's the former, then no, statics
don't have to be initialised [explicitly]. All statics are, in fact,
initialised upon program loading (or as part of program loading). So,
there is no need to initialise them. If the meaning of your "I thought
that..." is in fact that I said something to contradict this, what is it
you're referring to?
I guess that it may only pertain to class-level statics and not for the them that are available/defined at the namespace level.


"Class-level statics" have to be initialised at the namespace level _iff_
they aren't initialised in the class definition. Also, consider that the
statement that puts a static class member in play is usually simply
a *declaration*, and as with any object, a *definition* is required if
the object is used elsewhere.

The last but not least note. In the source code you posted along with
your original question there are *no* uninitialised objects. Look at
it once again. That's partially why I said that I wasn't sure what
*uninitialised* statics you were alluding to.
V


Victor,

Thanks for your response. Let me try to show another example:

#include <iostream>
namespace hi
{
static int namespaceStatic = 1; // allowed to be initialized here

class Hello
{
public:
static int classStatic = 10; // initialization is not allowed here
(see the compilation error below)
};
}
int main()
{
cout << hi::namespaceStatic << endl;
cout << hi::Hello::classStatic << endl;
}

/> g++ test.cc
test.cc:9: ANSI C++ forbids in-class initialization of non-const static
member `classStatic'
From the above example, it seems to me that you can initialize namespace
statics in the header while class-level statics MUST be initialized/defined
in the source file. Is that a fair statement to make?

Thanks,
Satish


Jul 23 '05 #5
"cppsks" <sk*****@hotmail.com> wrote...
[...] My issues is with the definition of "class definition
level" vs "namespace level". Can you categorize the namespace hi's
namespaceStatic as being at the "namespace level"?

#include <iostream>
namespace hi
{
static int namespaceStatic = 1; // allowed to be initialized here
}


The "namespace level" is particularly used to emphasise the necessity to
define the static data members outside of any class. Example:

// global namespace
class Outer {
class Intermediate {
class Inner {
static double value;
};
};
};

// Outer::Intermediate::Inner::value has to be defined somewhere. But
// you're not allowed to define it inside other classes, you need to get
// out of all classes to the namespace:

double Outer::Intermediate::Inner::value = 3.1415926;

// Now, if you do the same thing with the class hierarchy nestled in some
// namespaces,

namespace O {
namespace I {
class Outer {
class Intermediate {
class Inner {
static double value;
};
};
};
// you may define 'value' here
} // namespace I
// or here
} // namespace O
// or here. It does not matter
double O::I::Outer::Intermediate::Inner::value = 3.1415926;
-----------------------------------

HTH

V

Jul 23 '05 #6

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

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
2
by: free2cric | last post by:
Hi, I have two c++ static libraries say A.lib and B.lib. I am creating third static library C I create project in microsoft visual studio as win32 static library. I go to project-> settings ->...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
3
by: Jae | last post by:
Hello, here's what I want to do but I'm not sure if it will work because Im not sure how DB2 works with the JVM. Heres my setup: DB2---->UDF Class (lets call it A)----->Static Class (Lets call...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
25
by: gordon | last post by:
I aksed a few days ago about static methods and got some good answers that were really useful. Now I am not sure if about the use of static on members (variables) and classes. Can someone...
1
by: =?Utf-8?B?R2hpc3Rvcw==?= | last post by:
Hi all, I have a class with properties returning int, string, date... and generic Lists of other classes. private static string zipcode; private static string city; private static double? x;...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
38
by: Boon | last post by:
Hello group, I've been toying with a simple sudoku solver. I meant for the code to be short and easy to understand. I figured "brute force is simple" -- who needs finesse, when you've got...
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...
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...
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: 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
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.