On Tue, 13 Nov 2007 04:09:04 -0800,
subramanian100in@yahoo.com, India
wrote:
Quote:
Consider the following program:
>
#include <iostream>
>
using namespace std;
>
class Base
{
public:
Base(int x = 0);
>
private:
Base(const Base & arg);
>
int val;
static Base obj;
};
[snip]
Quote:
Base Base::obj = 9;
>
int main()
{
Base x = 1; // copy-initialization
>
return 0;
}
>
Suppose the above program is named x.cpp
>
When I compile this program under g++ with g++ -std=c++98 -pedantic
-Wall -Wextra x.cpp
>
I get compilation error for the following line in main(): Base x = 1;
>
This is because the copy ctor is private and copy-initialization is
involved.
But I do not get this error for the line: Base Base::obj = 9;
>
Why is the copy-initialization of class object as static member treated
differently? I do not understand the difference. Where I am going wrong
?
Notice that the static member is also private. During initialisation of
static members you have the access to all private and protected data.
Also keep in mind that for initialisations like
Base x = 1;
the compiler is required only to check the accessibility of copy
constructor but is free to optimise away the call to it.
--
Tadeusz B. Kopec (tkopec@NOSPAMPLEASElife.pl)
The road to Hades is easy to travel.
-- Bion