Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate object of a class before main() executes

Tags:

c++

Is it possible to instantiate an object of a class even before main() executes? If yes, how do I do so?

like image 939
Shree Avatar asked Oct 22 '10 10:10

Shree


1 Answers

Global objects are created before main() gets called.

struct ABC {

   ABC () {
      std::cout << "In the constructor\n";
   }
};

ABC s;  // calls the constructor

int main()
{

   std::cout << "I am in main now\n";
}
like image 101
Prasoon Saurav Avatar answered Nov 03 '22 04:11

Prasoon Saurav