Search code examples
c++classprogram-entry-pointglobal-object

Can in this way be declared a global object class?


I do not know why but this code works, what does this record }r; and how it works ? can in this way be declared a global object class ?

#include <iostream>

class А
{
    public:
        А()
        {
            std::cout << "Hello World";
        }
}r;

int main()
{

}

Solution

  • That declares a global variable named r that is of type A.

    It's the same as

    class A { ... };
    
    A r;
    
    int main() { ... }