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