S
subramanian100in
Consider the following program:
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
private:
Test(const Test & arg);
string str;
};
Test obj = string("test string");
int main()
{
return 0;
}
When I compile this program under g++, I get compilation error because
Test(const Test & arg);
is private.
However consider the following program.
#include <iostream>
#include <string>
using namespace std;
class Test
{
static Test obj;
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
Test(const Test & arg);
string str;
};
Test Test:bj = string("test string");
int main()
{
return 0;
}
This program compiles fine under g++ and produces the output
default ctor: test string.
The only difference is that the Test obj is static inside the Test
class in the second case.
Here also the copy ctor definition is not present.
However both programs compile fine under VC++2005 Express Edition and
produce the same result as above.
I do not understand.
Kindly explain.
Thanks
V.Subramanian
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
private:
Test(const Test & arg);
string str;
};
Test obj = string("test string");
int main()
{
return 0;
}
When I compile this program under g++, I get compilation error because
Test(const Test & arg);
is private.
However consider the following program.
#include <iostream>
#include <string>
using namespace std;
class Test
{
static Test obj;
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
Test(const Test & arg);
string str;
};
Test Test:bj = string("test string");
int main()
{
return 0;
}
This program compiles fine under g++ and produces the output
default ctor: test string.
The only difference is that the Test obj is static inside the Test
class in the second case.
Here also the copy ctor definition is not present.
However both programs compile fine under VC++2005 Express Edition and
produce the same result as above.
I do not understand.
Kindly explain.
Thanks
V.Subramanian