Connecting Tech Pros Worldwide Help | Site Map

initialization by copy

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 25th, 2007, 11:25 AM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
Default initialization by copy

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, 11:45 AM
Barry
Guest
 
Posts: n/a
Default 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, 02:15 AM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
Default 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, 10:15 AM
James Kanze
Guest
 
Posts: n/a
Default 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, 10:15 AM
James Kanze
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.