473,414 Members | 1,631 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,414 software developers and data experts.

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(Test&)'
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 2027
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(Test&)' 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****@NOSPAMPLEASElife.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...@NOSPAMPLEASElife.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(Test&)' 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
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...
6
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
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
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...
18
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
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> using namespace std; class Test { public:
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
9
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
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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
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...
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,...
0
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...

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.