0

Can someone please explain to me why the line A a2(); seems to ignore the constructor of my class? The following code:

#include <iostream>


class A
{
public:
    A() {
        std::cout << "Constructor of A" << std::endl << std::endl;
    }
};


int main() {
    std::cout << "a1" << std::endl << std::endl;
    A a1 = A();
    std::cout << "a2" << std::endl << std::endl;
    A a2();
    std::cout << "a3" << std::endl << std::endl;
    A a3;
    std::cout << "a4" << std::endl << std::endl;
    A a4{};

    return 0;
}

produces the following output:

a1

Constructor of A

a2

a3

Constructor of A

a4

Constructor of A

So A a2(); does not seem to call the constructor that I wrote, but I do not get why this is the case and all the other ways actually call it.

Thanks in advance!

charge
  • 11
  • 2

0 Answers0