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()
{}
Paul, you are still wrong, according to the C++ standard
Paul, let me quote the C++ standard exactly as you quoted:
Quote:
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.
Re: Paul, you are still wrong, according to the C++ standard
Quote:
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...