I am using Code::Blocks 10.05
with GCC
on Windows 7
. I was experimenting with C++ constructors
and I compiled and executed the following program.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"\n Constructor Invoked\n";
}
};
int main() {
base ob;
return 0;
}
The output was as expected and shown below.
Constructor Invoked
But while typing the program I accidentally compiled the following program. To my surprise it compiled without any error or warning.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"\n Constructor Invoked\n";
}
};
int main() {
base ob();
return 0;
}
But the program didn't give any output, just a blank screen. But no error or warning. Since it hasn't called the constructor I assume no object was created. But why no error or warning? Am I missing something very obvious?
When I added the line cout<<sizeof(ob);
I got the following error message.
error: ISO C++ forbids applying 'sizeof' to an expression of function type
So what is ob
? Is it considered as a function or an object?
Please somebody explain the line of code base ob();
and what actually happens in the memory when that line of code is executed?
Thank you.