S
subramanian100in
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;
};
Base::Base(int x) : val(x)
{
cout << "one arg ctor called" << endl;
}
Base::Base(const Base & arg) : val(arg.val)
{
cout << "copy ctor invoked" << endl;
}
Base Base:bj = 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:bj = 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 ?
Kindly explain.
Thanks
V.Subramanian
#include <iostream>
using namespace std;
class Base
{
public:
Base(int x = 0);
private:
Base(const Base & arg);
int val;
static Base obj;
};
Base::Base(int x) : val(x)
{
cout << "one arg ctor called" << endl;
}
Base::Base(const Base & arg) : val(arg.val)
{
cout << "copy ctor invoked" << endl;
}
Base Base:bj = 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:bj = 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 ?
Kindly explain.
Thanks
V.Subramanian