473,785 Members | 3,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy vs direct initialization

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:

Consider the program:
#include <iostream>

using namespace std;

class Test
{
public:
Test(int arg = 0) : val(arg) { }

explicit Test(Test &arg)
{
val = arg.val;
cout << "Test(Test &arg)" << endl;
}

explicit Test(const Test &arg)
{
val = arg.val;
cout << "Test(const Test &arg)" << endl;
}

private:
int val;
};

void fn(Test t)
{
return;
}

int main()
{
Test obj;
fn(obj);
return 0;
}

This program gives compilation error under g++ with
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
for the line
fn(obj);

The actual error message is
In function `int main()':
error: no matching function for call to `Test::Test(Tes t&)'
note: candidates are: Test::Test(int)
error: initializing argument 1 of `void fn(Test)'

However this program compiles fine with VC++ 2005 Express Edition.
It produces the output
Test(Test &arg)

Why doesn't the program compile with g++ ?
What is the expected behaviour ?

Kindly explain.

Thanks
V.Subramanian
Nov 23 '07 #1
2 2053
On Fri, 23 Nov 2007 00:28:16 -0800, su************* *@yahoo.com, India
wrote:
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:

Consider the program:
#include <iostream>

using namespace std;

class Test
{
public:
Test(int arg = 0) : val(arg) { }

explicit Test(Test &arg)
{
val = arg.val;
cout << "Test(Test &arg)" << endl;
}

explicit Test(const Test &arg)
{
val = arg.val;
cout << "Test(const Test &arg)" << endl; }

private:
int val;
};

void fn(Test t)
{
return;
}

int main()
{
Test obj;
fn(obj);
return 0;
}

This program gives compilation error under g++ with g++ -std=c++98
-pedantic -Wall -Wextra x.cpp for the line
fn(obj);

The actual error message is
In function `int main()':
error: no matching function for call to `Test::Test(Tes t&)' note:
candidates are: Test::Test(int) error: initializing argument 1 of
`void fn(Test)'

However this program compiles fine with VC++ 2005 Express Edition. It
produces the output
Test(Test &arg)

Why doesn't the program compile with g++ ? What is the expected
behaviour ?
Why do you declare copy constructors explicit? Without it everything
compiles fine. I have never seen explicit copy constructors and don't
know any purpose for them (to be honest I'm not sure what explicit in
this case means). Also presence of two copy constructors makes me
suspicious. If you know how to make a copy without modifying it's
argument value why support copying which modifies?

--
Tadeusz B. Kopec (tk****@NOSPAMP LEASElife.pl)
"Eat, drink, and be merry, for tomorrow you may work."
Nov 23 '07 #2
On Nov 23, 10:09 pm, "Tadeusz B. Kopec" <tko...@NOSPAMP LEASElife.pl>
wrote:
On Fri, 23 Nov 2007 00:28:16 -0800, subramanian10.. .@yahoo.com, India
wrote:


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:
Consider the program:
#include <iostream>
using namespace std;
class Test
{
public:
Test(int arg = 0) : val(arg) { }
explicit Test(Test &arg)
{
val = arg.val;
cout << "Test(Test &arg)" << endl;
}
explicit Test(const Test &arg)
{
val = arg.val;
cout << "Test(const Test &arg)" << endl; }
private:
int val;
};
void fn(Test t)
{
return;
}
int main()
{
Test obj;
fn(obj);
return 0;
}
This program gives compilation error under g++ with g++ -std=c++98
-pedantic -Wall -Wextra x.cpp for the line
fn(obj);
The actual error message is
In function `int main()':
error: no matching function for call to `Test::Test(Tes t&)' note:
candidates are: Test::Test(int) error: initializing argument 1 of
`void fn(Test)'
However this program compiles fine with VC++ 2005 Express Edition. It
produces the output
Test(Test &arg)
Why doesn't the program compile with g++ ? What is the expected
behaviour ?

Why do you declare copy constructors explicit? Without it everything
compiles fine. I have never seen explicit copy constructors and don't
know any purpose for them (to be honest I'm not sure what explicit in
this case means).
bingo.I guess this is the source of the problem.
Also presence of two copy constructors makes me
suspicious. If you know how to make a copy without modifying it's
argument value why support copying which modifies?
that may be a design issue.

regards,
FM.
Nov 23 '07 #3

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

Similar topics

6
3017
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>
6
7519
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
11
580
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
7
1787
by: pauldepstein | last post by:
A book I have describes a class called vector. It then explains (and I understand) the concept of a copy constructor, which is needed to interpret statements like vector b = a; But then I get confused when it speaks of overloading =, and explains that this concept is needed to interpret statements like b = a;
18
1345
by: Patrick Kowalzick | last post by:
Hello all, find below an example which IMO should refuse to compile. It compiles fine with VS2005. What happens in the call? What is your opinion? Kind regards, Patrick
3
1716
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> using namespace std; class Test { public:
2
1836
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
9
2316
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
7
163
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test { public: Test() { cout<<"Cons\n";} Test(Test& a) { cout<<"Copy cons\n";}
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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
10147
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
10085
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
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4045
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
2
3645
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.