Constructor

I know in main() should be A a1 instead of A a1()
but I'm curious why A a1() doesn't do anything?
What does a1() call?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

class A {

private:
  int x;
public:
  A() : x(x){  cout << "Constructor"; }
  ~A()  {cout << "destructor"; }
};


int main() {
A a1();
}
It declares a function named a1 that returns a value of type A; this is known as "the most vexing parse" if you are interested in Googling it.
A a1(); is the declaration of a function named a1 that takes no arguments and returns an object of type A.
Thanks. so if a1() is a function and I add something like
void a1(){
cout << "HEY";
}
in the class why doesn't print HEY?!
Last edited on
All that line does is declare a function. It does not actually call anything. If you want to call a1(), you will need to do that explicitly.
Presumably you meant a1 and not z1. As I said, it's a function declaration. It is not calling a function. If it were calling a function and the function didn't exist, you would encounter a linking error.
Topic archived. No new replies allowed.