copy vs direct initialization

S

subramanian100in

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
 
T

Tadeusz B. Kopec

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?
 
T

terminator

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top