473,614 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1431
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.comwro te:
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 objektorientier ter Datenverarbeitu ng
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.comwro te:
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 objektorientier ter Datenverarbeitu ng
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
2995
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 says). But in the example below the copy constructor of Vec is executed when initializing the Ref object. Is this standard conform? Doesn't it result in bad performance? #include <iostream>
17
2070
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 of the following notations: MyClass instance1 = MyClass(1, 2, 3); MyClass instance2(4, 5, 6); What's curious: the second notation variant is called an "abbreviated form"
7
3726
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
8543
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
3647
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 the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
4
3701
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
2
2373
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 : explicit auto_ptr(_Ty *_Ptr = 0) _THROW0() the second : auto_ptr(auto_ptr_ref<_Ty_Right) _THROW0()
2
1823
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
2
2045
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 construct arg ? The reason for asking this question is the following:
11
2397
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 happens here Suppose we have a function void fn(Test arg);
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8642
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8591
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8294
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7115
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.