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()
{}
stober
August 17th, 2002, 06:18 PM
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.
class COk
{
public:
COk() {x = 0; }
static int x;
static Base _base;//the static here make difference.
};
COk::x = 0;
void main()
{
}
Paul McKenzie
August 17th, 2002, 06:23 PM
My comments:
#include <iostream.h>
This should be
#include <iostream>
Use the standard header <iostream>, not iostream.h.
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.
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.
int main()
{}
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
AnthonyMai
August 18th, 2002, 09:19 AM
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.
Andreas Masur
August 18th, 2002, 09:44 AM
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.
pardxa
August 18th, 2002, 01:27 PM
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.:D
Paul McKenzie
August 18th, 2002, 01:37 PM
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.5There shall be exactly one definition of a static data member that is used in a programDo you post just to hear yourself talk? Your post added absolutely nothing except confusion.
Regards,
Paul McKenzie
Paul McKenzie
August 18th, 2002, 01:47 PM
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.:D :mad: 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
Paul McKenzie
August 18th, 2002, 02:25 PM
Of course, "AM" does not mean Andreas Masur. Welcome back, Andreas. :)
Regards,
Paul McKenzie
Andreas Masur
August 18th, 2002, 04:37 PM
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... :p 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.:D
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.
Paul McKenzie
August 18th, 2002, 07:07 PM
That should be section 9.4.2.5 of the ANSI standard, not 9.2.4.5.
Regards,
Paul McKenzie
PaulWendt
August 19th, 2002, 11:36 AM
Originally posted by AnthonyMai
Paul, you are wrong.
You've failed again, Mai. Better luck next time.
--Paul
AnthonyMai
August 19th, 2002, 03:54 PM
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.
stober
August 19th, 2002, 04:08 PM
You're right. Such a simple thing to prove -- this compiles ok with VC6
#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:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Cfoo foo;
Cfoo::something = 0;
return 0;
}
Andreas Masur
August 19th, 2002, 04:16 PM
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...
Andreas Masur
August 19th, 2002, 04:27 PM
Originally posted by stober
You're right. Such a simple thing to prove -- this compiles ok with VC6...
The implementation of the standard has to be done by the compiler. Visual C++ is known as the worst one regarding the implementation of the C++ specification. Nevertheless I guess even your first example will give you a warning on level 4. But what about another compiler? Besides that I can force every warning to become an error in Visual C++...
But the discussion was not about the intelligence of any compiler, it was about a simple specification issue. And as I and other already said it is pretty easy. It MUST be defined. Always. As stated in the book 'The C++ Programming Language'. Written by the inventor of C++. Period.
I could also ask another question...Why would I declare a static member variable without using it??? It would be just like buying a car without having a driver's license...
stober
August 19th, 2002, 05:04 PM
Originally posted by Andreas Masur
The implementation of the standard has to be done by the compiler. Visual C++ is known as the worst one regarding the implementation of the C++ specification. Nevertheless I guess even your first example will give you a warning on level 4. But what about another compiler? Besides that I can force every warning to become an error in Visual C++...
Yes, I thought about that too just after I hit the "Submit Reply" button.
I could also ask another question...Why would I declare a static member variable without using it??? It would be just like buying a car without having a driver's license...
I've seen a lot of code where the programmer declared something in a class then later did not use it. Seems like pretty sloppy coding to me, but if they HAD to be declared it might break a lot of lagecy programs.
So much for my 2 cents worth. I'm not an expert on this, so I'll just shut up and let you all battle it out.
AnthonyMai
August 19th, 2002, 07:01 PM
Andreas, please read after me the exact sentence Paul quotes from the C++ standard:
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.
There shall be exactly one definition of a static data member that is used in a program.
There shall be exactly one definition of a static data member THAT IS USED in a program.
Got it?
As for your accusation that I did not read Paul's origial answer, on commenting my word "Had the standard said any thing like "It must be defined". My answer is I did notice Paul quoted a "must" word from Stroustop's book. Please note that Stroustrup's book DOES NOT EQUAL TO THE C++ STANDARD, even though he himself is in the standard committee.
There is not a "must" in the exact sentence in the standard C++ document. It says very clearly that WHEN it is USED, the static class member SHALL be defined exactly once. There is a conditional modifier in that sentence, the condition is when the class member is referenced or used.
There is no point defining something that is never used at all. And the C++ standard does NOT force you to define something that you don't use, even if it is declared.
To stobe, there are legitimate cases where something is declared but never used. You may have one header file, shared by two different applications, if you have a static class member which is used in one application but not the other, you have to declared in in the header file, but you do NOT need to define it any where in the second application where it is never referenced.
Andreas wonders why would one buy a car without a driver license. There are no less than a trillion legitimate reasons who one would buy a car even without a driver license. It's a pity that Andreas's intelligence doesn't allow him to even think of one of those reasons. Try a car rendal company? You don't need a driver license to operate a car rental company, since you do not drive the cars yourself :-)
Paul McKenzie
August 19th, 2002, 08:39 PM
Originally posted by Andreas Masur
The implementation of the standard has to be done by the compiler. Visual C++ is known as the worst one regarding the implementation of the C++ specification. Nevertheless I guess even your first example will give you a warning on level 4. But what about another compiler? Besides that I can force every warning to become an error in Visual C++...
But the discussion was not about the intelligence of any compiler, it was about a simple specification issue. And as I and other already said it is pretty easy. It MUST be defined. Always. As stated in the book 'The C++ Programming Language'. Written by the inventor of C++. Period.
I could also ask another question...Why would I declare a static member variable without using it??? It would be just like buying a car without having a driver's license... I made futher reading into this. The standard states that "no diagnostic is required" if the static member's definition is omitted. This is whether you use the static member or not. The code that praxda originally posted could fail to link on another compiler, since the standard doesn't state that a diagnostic "shouldn't be given", only that one is not required. Conversely, if the definition is left out and the static member is used, a compiler (more than likely, the linker) doesn't have to issue a diagnostic, since, again, one is not required. This is according to my reading of the standard, clause 9.4.2.5.
Here is clause 1.4.2 concerning what is meant by "no diagnostic is required":
If a program contains a violation of a rule for which no diagnostic is required, this International Standard
places no requirement on implementations with respect to that program.Therefore to avoid this, the coder should define the static members. Stroustrup, in all likelihood, states that static members must be defined so as to avoid implementations that do not issue diagnostic messages, leaving your program open to undefined behavior.
Regards,
Paul McKenzie
AnthonyMai
August 20th, 2002, 03:35 AM
We all agree that when a static class member is USED, it needs to be defined just once. We disagree with what happens when it is NEVER used any where, in that case whether it should be defined or not.
The C++ standard leaves it open, it does NOT require diagnostics and leaves it to the compilers. But that doesn't mean compilers will implement it in any arbitrary unreasonable way.
Let me make an analog here. The law does NOT require you to report loss of your own private property, nor does it say you shouldn't report your property loss. It leave it open to your own decision. Does that mean you can abuse the system? The law doesn't prohibit you from reporting a loss, however small that may be. But it would be totally ridiculous to report to the police that you have lost a dollar, although it is perfectly legal to do do. On the other hand, also the law does NOT force you to report it to police if you have lost a brand new luxury car, there is hardly any idiot who would see his vehicle stolen without reporting it.
Back to the issue of omitted static class member definition. If the static member is never used at all, there is really no difference whether it is defined or not, except in the case when it is not defined, you save some code size. I hardly see any reason why a compiler should complain. Actually the vast majority of compilers will happily omit even the stuff that you explicitly define in your code, when it deems that the defined stuff is never referenced any where. Can a compiler issue a warning of unreferences and undefined static class member? Sure it can, just as you are completely entitled to the civil right to report a one dollar loss. But it's so silly that no one does it.
In the case where the static class member is not defined, but you do use it in the code, the standard does NOT require the diagnistics, either. But it is almost automatic that all compilers will give an error message. It simply can not link. It is used, hence it must be linked, and if it is not defined, there is nothing to link. Can a compiler ignore the problem and does not issue a diagnistics? It surely is perfectly legal to do so, just like you are not legally required to report loss of your vehicle. But it makes no sense not to report the error or not to report loss of your vehicle.
Another silimar thing is unreferenced local variables. Does un-referenced local variables break any C++ rules? It doesn't. It is perfectly legal standard C++ code. But virtually all compilers will issue a warning about unreferences local variables. They are useless, but probably they don't hurt much except taking up un-necessary stack space, and they do not violate any C++ standard. But they are worth a warning nevertheless.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.