|
-
August 17th, 2002, 05:54 PM
#1
question about static member
when I define some classes like following:
class Base
{
public:
Base(){cout<<"come from Base";}
};
class COk
{
public:
Base _base;
};
there can be a string displayed which proved the _base object can be initialized.but when I add static in it:
class Base
{
public:
Base(){cout<<"come from Base";}
};
class COk
{
public:
static Base _base;
};
nothing displayed,
First, I want to ask why? I know when a member in a class marked static, there will be only one copy kept even if the class produced many objects.but why the initializtion cann't carry out?please give me explaination on how compiler reacts when meet static object in a class or show me relevant articles to refer.thank you in advance.
The next question is How can I carry out the initialization of a static member?
my program followed:
#include<iostream.h>
class Base
{
public:
Base(){cout<<"come from Base";}
};
class COk
{
public:
static Base _base;//the static here make difference.
};
COk _myCok;
void main()
{}
-
August 17th, 2002, 06:18 PM
#2
I can't quote chaper and verse of the C++ standard, but static class objects are not initialized at run-time, but at compile time. For example, add another static member to COk as shown below. Notice that the interger initialization is done outside the class, just like any other globally defined variable.
Code:
class COk
{
public:
COk() {x = 0; }
static int x;
static Base _base;//the static here make difference.
};
COk::x = 0;
void main()
{
}
-
August 17th, 2002, 06:23 PM
#3
My comments:
Code:
#include <iostream.h>
This should be
Code:
#include <iostream>
Use the standard header <iostream>, not iostream.h.
Code:
class Base
{
public:
Base(){std::cout<<"come from Base";}
};
class COk
{
public:
static Base base; //do not use underscores preceding variable names.
};
The compiler may use internal variables with leading underscores, therefore you should avoid doing it for your own variables.
Code:
COk myCok;
Base COk::base; // statics must be defined
You didn't define the static member. It is not enough to just declare static member variables in your class -- they must also be defined. This is why your static variable is not being instantiated, and a good linker should have pointed this out to you.
It is "int main()", not "void main()". A proper C++ program defines main() as returning an int. By using "void main()", your program will produce a compiler error on many non-VC++ compilers.
Given all this, try again with the changes above.
Regards,
Paul McKenzie
-
August 18th, 2002, 09:19 AM
#4
You didn't define the static member. It is not enough to just declare static member variables in your class -- they must also be defined. This is why your static variable is not being instantiated, and a good linker should have pointed this out to you.
Paul, you are wrong. If it compiles without a problem, that means in the specific case of the OP, declaring a static member variable without defining it in one of the CPP file is ENOUGH. In most cases (but nor in all cases) you do need to define static member variables, in such cases if you don't define static member variables, the compiler will complain about unresolved external. If it does not complain, that means it is OK not to define the static variable, which is the case of the OP.
Why? Because that static member variable is NEVER referenced any where in the code. Hence no instance of that static member variable is ever instantiated. It's never instantiated at compile time, it's never constructed at run time. It never existed. Hence the OP never saw the constructor being called.
On the contrary to what Paul believes, I think in the case of a unreferenced static class member, it is better to leave it declared and not defined. Certainly it will be best to remove the declaration altogether. Why declare and define something you never use at all?
Compilers give warnings of "unreferenced local variables". I guess they should also give the same warnings about un-referenced global variables, static and none-static member variables, class member functions etc., anything unreferenced, if you are building a complete application instead of a library.
Unfortunately compiling individual files and linking them are two separate steps. When individual files are compiled, the compiler does not know whether the file is to be compiled into application or library at the end of day, so it can not give a warning even if it find something is never referenced.
-
August 18th, 2002, 09:44 AM
#5
Originally posted by AnthonyMai
Paul, you are wrong.
Aaahhhh, here we go again...It is really funny...coming back to this forum after a while and still finding your answers...at least you created a new user...
Okay...just as most of the time...YOU ARE WRONG!!! The C++ standard clearly states that every static class member (Data and Functions) MUST be defined. It is that easy.
If you declare a static class member you have to define it as well - disregarding whether it is used or not. There is no discussion about it. If you use a language you have to follow its rules - if you like it or not.
Me and others as well have told you several times that you should stop posting such kind of stupid things. You again made a valid answer wrong to the original poster. If you do not understand the basic things just do not post to a topic.
-
August 18th, 2002, 01:27 PM
#6
hi,everybody.PLEASE CALM YOURSELF.there is a saying:it is the mortal's misfortune when gods come into conflict.
PLEASE show me your evidence. I would like to download a c++ standard and give a thorough read. who can give an offer,my experts? and thank you for your given helping hand.
-
August 18th, 2002, 01:37 PM
#7
Originally posted by AnthonyMai
Paul, you are wrong.
What a load of nonsense. As Andreas points out, static members must be defined. Here is the excerpt from "The C++ Programming Language", Stroustrup 3rd edition, Section 10.2.4.
Static members - both functions and data members -- must be defined somewhere.
From the ANSI specification, section 9.2.4.5
There shall be exactly one definition of a static data member that is used in a program
Do you post just to hear yourself talk? Your post added absolutely nothing except confusion.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 18th, 2002 at 01:50 PM.
-
August 18th, 2002, 01:47 PM
#8
Originally posted by pardxa
hi,everybody.PLEASE CALM YOURSELF.there is a saying:it is the mortal's misfortune when gods come into conflict.
PLEASE show me your evidence. I would like to download a c++ standard and give a thorough read. who can give an offer,my experts? and thank you for your given helping hand.
I was afraid of this. A comment by AM adds confusion to a very simple concept. I still remember the "it's OK to return a reference to a local variable" foolishness. This forum is to help the beginner, but it seems that someone wants to sabotage any answer that myself and a couple of others give, just to hear himself spout a lot of hot air.
pardxa, the rule is very simple. Static members must be defined, therefore your program is ill-formed. I pointed out the section in the ANSI standard as well as the excerpt from "The C++ Programming Language", written by the inventor of C++, Bjarne Stroustrup. There is no dispute, and as I stated, a very simple C++ rule.
Regards,
Paul McKenzie
-
August 18th, 2002, 02:25 PM
#9
Of course, "AM" does not mean Andreas Masur. Welcome back, Andreas. 
Regards,
Paul McKenzie
-
August 18th, 2002, 04:37 PM
#10
Originally posted by pardxa
hi,everybody.PLEASE CALM YOURSELF.there is a saying:it is the mortal's misfortune when gods come into conflict.
Well...it is okay...I am fine... This is definitely not a question about some kind of gods. I would never ever consider myself as one. The problem is that this comment made by Anthony is simply wrong and off-topic and that is is not the first time.
You are pretty new to this forum so you do not know him but Paul, myself and many others here know this guy already for a longer time. He is always answering in a unsuspecting, rude manner just showing that he does not know what he is talking about most of the time. Do not get me wrong...it is nothing bad about not knowing everything. But I consider it bad if a person is posting answers which are simply wrong because of missing knowledge and also has the impudence saying that valid and correct answers are wrong. Basically I do not have that big of a problem with that but a beginner who is trying to learn the language will be confused and will probably use the wrong syntax etc. provided by such an answer and that cannot be the purpose of this forum.
Originally posted by pardxa
PLEASE show me your evidence. I would like to download a c++ standard and give a thorough read. who can give an offer,my experts? and thank you for your given helping hand.
Paul already gave you the corresponding sections in the book "The C++ Programming Language", Stroustrup 3rd edition and from the ANSI standard. Do a search on e.g. Google and you will find other links to sites which explain the topic pretty well.
-
August 18th, 2002, 07:07 PM
#11
That should be section 9.4.2.5 of the ANSI standard, not 9.2.4.5.
Regards,
Paul McKenzie
-
August 19th, 2002, 11:36 AM
#12
Originally posted by AnthonyMai
Paul, you are wrong.
You've failed again, Mai. Better luck next time.
--Paul
-
August 19th, 2002, 03:54 PM
#13
Paul, you are still wrong, according to the C++ standard
Paul, let me quote the C++ standard exactly as you quoted:
Paul said: From the ANSI specification, section 9.2.4.5
quote:
--------------------------------------------------------------------------------
There shall be exactly one definition of a static data member that is used in a program
--------------------------------------------------------------------------------
What does "that is used" mean? It means it is referenced and used in a program. If it is used it must be defined. On the other hand, if it is NOT used, the C++ standard does NOT require that it be defined.
Had the standard said any thing like "It must be defined", or something like "It must be defined, regardless whether it is used or not", I would have been wrong.
But the standard CLEARLY added the condition modifier "that is used in a program". So, Paul, you lose this one.
-
August 19th, 2002, 04:08 PM
#14
You're right. Such a simple thing to prove -- this compiles ok with VC6
Code:
#include "stdafx.h"
class Cfoo
{
public:
Cfoo() {}
~Cfoo() {}
static int something;
int something_else;
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Cfoo foo;
foo.something_else = 0;
return 0;
}
However, if I change it like this, the compiler produces unresolved externals link errors:
Code:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Cfoo foo;
Cfoo::something = 0;
return 0;
}
-
August 19th, 2002, 04:16 PM
#15
Re: Paul, you are still wrong, according to the C++ standard
Originally posted by AnthonyMai
Had the standard said any thing like "It must be defined"...
Do you ever read the answers provided to your nonsense posts??? It does not look like. May be you can give me your phone number and I will read my answers to you since you are obviously not been able to read...at least not the English language.
Paul and myself clearly pointed out that the standard clearly states that a static member MUST be defined. Paul even added the appropriate section in the book "The C++ Programming Language". What you are trying to do is rescuing yourself by picking on some words since you already have realized that you do not know anything you are talking about most of the time when it comes down to the C++ language.
I asked you several times to stop posting such kind of answers and provide useful and mainly correct answers instead but it seems that you are not capable or at least not willing to do this. As I said already yesterday: Answers like yours are not the purpose of this forum and therefore I will warn you the last time:
Stop posting those answers or otherwise I will get the moderators attention to these kinds of threads...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|