Here is my code
#include <iostream>
using namespace std;
class Q
{
public:
Q() { cout << "constructor" << endl; }
};
extern const Q t/*()*/; //A
const Q s/*()*/; //B
int main()
{
const Q t/*()*/;
}
I expect the line marked "A" to mean that I am creating an object t of type Q whose linkage is external, and whose fields can't be modified. The creation is done by the constructor with no arguments, which I have provided.
In main, I expect that a local t object of type Q is created in the same way, though its linkage will necessarily be just this file, and in fact, its scope and duration will just be for main.
C++ allows me to put or not put the parenthesis in const Q t/()/; in main. In global scope, in line A I can put or not put the parentheses, and the constructor will not be called either way.
In line B, I am only allowed to not put the parenthesis since otherwise, the compiler would be confused if I am defining a function prototype.
My questions are:
Why am I allowed the flexibility to put the () or not in line //A, considering that in line //B this flexibility does not exist?
Regardless of my choice in 1., I find that "constructor" is not printed by line A, even though it is printed in line B. Why? I don't really have an expectation on this. On the one hand, it would be reasonable to print it since we see in main that C++ understands to call the 0-argument constructor even without the parentheses. On the other hand, if that were the case, then how would I ever do a non-defining declaration of a class type?