-1

When calling the constructor with a default constructor argument no object gets constructed.

    class cl{
private:
public:
    cl(){cout << "Default used" << endl;};
    cl(const cl & cl_object) {cout << "Copy used" << endl;};
    cl & operator=(const cl & cl_object){cout << "Assignment used" << endl; return *this;};
};

When I write:

cl(cl()); 

no message gets displayed.

Questions: 1) Why no object is constructed ? 2) Why the copy constructor is not used ?

Nameless
  • 143
  • 6
  • @NathanOliver Can you explain how this is a duplicate ? – Nameless Oct 20 '17 at 11:46
  • Like the answer says, `cl(cl());` declares a function. Since it is a function and not a variable you're not going to see any constructor calls since you don't construct anything. – NathanOliver Oct 20 '17 at 11:47
  • @NathanOliver So if I am understanding it correctly using cl(); only constructs an object but using cl(cl()); declares a function. But if it declares a function shouldn't the compiler output an error since I didn't specify the return type ? – Nameless Oct 20 '17 at 11:49

1 Answers1

1

Most vexing parse

cl(cl());

is parsed as function declaration.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Shouldn't the compiler output an error since I didn't specify the return type ? – Nameless Oct 20 '17 at 11:56
  • My understanding is that it is a function called cl with argument of type class cl. But it still has no return type. In my compiler if I don't specify the return type it returns an error. So what happens here ? – Nameless Oct 20 '17 at 11:57
  • Is is parsed as `c1 c1()` so function `c1` (which hides class `c1`) with return type `c1` (the class). – Jarod42 Oct 20 '17 at 12:03
  • Hmm.. Thanks for the clarification. If possible could you add that to your answer for other people with the same question ? – Nameless Oct 20 '17 at 12:05
  • There is a duplicate. So I expected the redirection of flow. – Jarod42 Oct 20 '17 at 12:48