473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Sep 25 '07 #1
4 1420
su**************@yahoo.com, India wrote:
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
Sep 25 '07 #2
On Sep 25, 4:41 pm, Barry <dhb2...@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
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
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

Sep 26 '07 #3
On Sep 25, 1:23 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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::obj = 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.

--
James Kanze (GABI Software) email:ja*********@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

Sep 26 '07 #4
On Sep 26, 4:07 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
On Sep 25, 4:41 pm, Barry <dhb2...@gmail.comwrote:
[...]
Doesn't it look a little like singleton pattern?
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:ja*********@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

Sep 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
17
by: Thomas Lorenz | last post by:
Hello, I'm a little confused about object initialization. According to Stroustrup (The C++ Programming Language, Special Edition, Section 10.2.3) constructor arguments should be supplied in one...
7
by: skishorev | last post by:
What is the difference between copy initialization and assignment. How the memory will allocates the objects. Thanks &regards, Sai Kishore
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
2
by: subramanian100in | last post by:
Suppose we have a class named Test. We have a function void fn(Test arg); When this function is called, what kind of initialization - direct initialization or copy initialization, happens to...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.