Connecting Tech Pros Worldwide Help | Site Map

initialization by copy

  #1  
Old September 25th, 2007, 12:25 PM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
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::obj = 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

  #2  
Old September 25th, 2007, 12:45 PM
Barry
Guest
 
Posts: n/a

re: initialization by copy


subramanian100in@yahoo.com, India wrote:
Quote:
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::obj = 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?


--
Thanks
Barry
  #3  
Old September 26th, 2007, 03:15 AM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a

re: initialization by copy


On Sep 25, 4:41 pm, Barry <dhb2...@gmail.comwrote:
Quote:
subramanian10...@yahoo.com, India wrote:
Quote:
Consider the following program:
>
Quote:
#include <iostream>
#include <string>
>
Quote:
using namespace std;
>
Quote:
class Test
{
public:
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
>
Quote:
private:
Test(const Test & arg);
string str;
};
>
Quote:
Test obj = string("test string");
>
Quote:
int main()
{
return 0;
}
>
Quote:
When I compile this program under g++, I get compilation error because
Test(const Test & arg);
is private.
>
Quote:
However consider the following program.
#include <iostream>
#include <string>
>
Quote:
using namespace std;
>
Quote:
class Test
{
static Test obj;
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
Test(const Test & arg);
string str;
};
>
Quote:
Test Test::obj = string("test string");
>
Quote:
int main()
{
return 0;
}
>
Quote:
This program compiles fine under g++ and produces the output
default ctor: test string.
>
Quote:
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.
>
Quote:
However both programs compile fine under VC++2005 Express Edition and
produce the same result as above.
>
Quote:
I do not understand.
>
Doesn't it look a little like singleton pattern?
>
--
Thanks
Barry
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

  #4  
Old September 26th, 2007, 11:15 AM
James Kanze
Guest
 
Posts: n/a

re: initialization by copy


On Sep 25, 1:23 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Quote:
Consider the following program:
Quote:
#include <iostream>
#include <string>
Quote:
using namespace std;
Quote:
class Test
{
public:
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
Quote:
private:
Test(const Test & arg);
string str;
};
Quote:
Test obj = string("test string");
Quote:
int main()
{
return 0;
}
Quote:
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.
Quote:
However consider the following program.
#include <iostream>
#include <string>
Quote:
using namespace std;
Quote:
class Test
{
static Test obj;
Test(const string & x = "") : str(x) { cout << "default ctor: " << str
<< endl; }
Test(const Test & arg);
string str;
};
Quote:
Test Test::obj = string("test string");
Quote:
int main()
{
return 0;
}
Quote:
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.
Quote:
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).
Quote:
However both programs compile fine under VC++2005 Express
Edition and produce the same result as above.
Quote:
I do not understand.
VC++ has a bug.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  #5  
Old September 26th, 2007, 11:15 AM
James Kanze
Guest
 
Posts: n/a

re: initialization by copy


On Sep 26, 4:07 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Quote:
On Sep 25, 4:41 pm, Barry <dhb2...@gmail.comwrote:
[...]
Quote:
Quote:
Doesn't it look a little like singleton pattern?
Quote:
I am a beginner in C++. I do not know about singleton pattern.
Get the book "Design Patterns", and read it. It's one of the
musts for any programmer.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of function argument subramanian100in@yahoo.com, India answers 11 November 27th, 2007 08:45 PM
Copy Constructor and Initialization by Temporaries anujanujdhamija@gmail.com answers 8 January 4th, 2006 03:35 PM
Initialization of array by vector Alex Vinokur answers 5 July 22nd, 2005 06:14 AM