0
class MyClass{
public:
   MyClass();
   MyClass(const MyClass& my);
   MyClass(MyClass&& my);
};

void func(MyClass my);

int main()
{
   func(MyClass());
   MyClass myClass(MyClass());


   return 0;
}

func(MyClass()) runs fine. And here, MyClass() is an anonymous instance. But MyClass myClass(MyClass()) is a function, not an object. And here, MyClass() is a pointer to function. I think it should call the copy constructor but it doesn't. why?

*note : I know myClass is a function if it has empty parentheses.

user98396
  • 29
  • 4
  • 5
    `MyClass myClass(MyClass())` is a function *declaration*, like `int foo(int);`. By itself it doesn't do anything. – HolyBlackCat Aug 28 '20 at 06:59
  • 3
    Lookup the [Most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse). – dxiv Aug 28 '20 at 07:27
  • 1
    As an alternative solution, you can prefer syntax like: `MyClass myClass{MyClass()}` if you have C++11 or later, and `MyClass` does not have an initializer list constructor, or you can just use `MyClass myClass((MyClass()))` to force it thinking `MyClass()` as an object. Also [here for some reasoning about why "Most vexing parse" was introduced](https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse) . – Ranoiaetep Aug 28 '20 at 07:42

0 Answers0