initialization by copy

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::eek: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
 
B

Barry

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::eek: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.

Doesn't it look a little like singleton pattern?
 
S

subramanian100in

Doesn't it look a little like singleton pattern?

I am a beginner in C++. I do not know about singleton pattern.
So kindly explain the difference between the above two programs.
Please excuse me if I am wrong in asking it.

Thanks
V.Subramanian
 
J

James Kanze

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.

Normal. Since it is private, you can only access it within the
class itself.

Note that if you write:
Test obj( std::string( "test string" ) ) ;
there should be no problem.
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::eek:bj = string("test string");
int main()
{
return 0;
}
This program compiles fine under g++ and produces the output
default ctor: test string.

Right. The difference is that here, obj is a member of Test, so
it has a right to use private functions in its initializer.
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.

Which means, formally, that your program has undefined behavior
in both cases. Practically, in the case of copy initialization
(initialization specified by means of the '=' token), the
compiler is explicitly allowed to elide the copy as an
optimization measure (even if the copy constructor has side
effects that affect the observable behavior of the program!),
and most compilers do, most of the time. But an accessible copy
constructor is still required to be present, and is formally
considered "used". Failure to provide a definition for a
function that is "used" is undefined behavior---in all of the
systems I know, however, it will cause a linker error if the
function is really used (as opposed to just formally "used"),
and the code will work if it is not actually used (as is the
case here).
However both programs compile fine under VC++2005 Express
Edition and produce the same result as above.
I do not understand.

VC++ has a bug.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top