Reputation: 317
#include <iostream>
using namespace std;
class test{
public:
test() { cout<<"CTOR"<<endl; }
~test() { cout<<"DTOR"<<endl; }
};
int main()
{
test testObj();
cout<<"HERE"<<endl;
}
Output:
HERE
Compiler skips the line "test testObj(); " and compiles the rest with warning and when run will generate the output. The warning is "prototyped function not called (was a variable definition intended?) in VC++ 2008. Why does it not throw an error?
Upvotes: 1
Views: 124
Reputation: 10285
Remove the () from the constructor call in Main
int main()
{
test testObj;
cout<<"HERE"<<endl;
}
Upvotes: 1
Reputation: 791361
Simply, because it's not an error to declare a function such as the one you declared. The warning should be useful enough, though.
Upvotes: 3
Reputation: 272447
Because it's not an error.
Your code has fallen foul of the most-vexing parse (in summary, test testObj();
doesn't define a variable, it declares a function).
Upvotes: 8