Constructor call

Why (1) shows an error and (2) does not? (Please don't post comments/corrections that are off-topic and spoil my thread.)


(1)
#include <iostream>
using namespace std;

class XYZ
{
public:
XYZ()
{
cout << "const XYZ called";
}
};


class ABC
{

public:

ABC(XYZ xyz)
{
cout << "const ABC called";
}
void print()
{
}
};


int main()
{
ABC b(XYZ());
b.print();
}


(2)
#include <iostream>
using namespace std;

class XYZ
{
public:
XYZ()
{
cout << "const XYZ called";
}
};


class ABC
{

public:

ABC(int i, XYZ xyz)
{
cout << "const ABC called";
}
void print()
{
}
};


int main()
{
ABC b(1, XYZ());
b.print();
}

When you get a compiler error, you should always post the error message.
In any case, ABC b(XYZ()); is a function declaration, which is probably not what you intended.

Please don't post comments/corrections that are off-topic and spoil my thread.

That sort of comment is unnecessary. What needs to be said will be said.
Topic archived. No new replies allowed.