Pat
Pat

Reputation: 331

Declaring a Global Class Instance in a C++ file

I though I had a good understanding of how C++ worked but I'm confused about a piece of its use. If I declare a class instance globally in a .cpp file (not associated with a class) like

Class b(constructor parameters)

it doesn't cause a problem. My understanding was that declaring classes this way allocated them in the stack frame for the method they were in instead of on the heap. But if I declare this globally there is no method and thus no stack frame right? Why am I allowed to do this and more importantly what is happening and is this any kind of big no no in C++?

Upvotes: 1

Views: 10708

Answers (3)

umlcat
umlcat

Reputation: 4143

Quick Comment

You may want to change your post's title to:

Declaring a Global Class Instance in a C++ file ?

or:

How does a global object, its declared, and, later, stored in memory, in C++ ?

I may be wrong, but, seems to me, like you have work with other Object Oriented, programming languages, reference based, and try to apply your experience on C++.


Example

C++, its a mix of: Procedural Programming & Object Oriented Programming, and some other stuff, like functional.

You may want to see a Procedural Program, as a single object, which class has been already declared & make an instance. and, has this public main method, automatically executed, like a constructor.

When you read a C++ file like this:

// file: "example.cpp"

// class declaration without local variables
class CoordinateClass
{
  int x;
  int y;
};

// a global variable
CoordinateClass MyCoordinates;

int main (char[][] args)
{
  // dont care about this in this example
  int ErrorCode = 0;

  // DoSomething();

  // dont care about this in this example
  return ErrorCode;
} // int main()

You may want to think as this:

// file: "example.cpp"

public: class example: program
{
  public:
    // class declaration without local variables
    class CoordinateClass
    {
      int x;
      int y;
    };

   // a global variable, works like a field of a class
   CoordinateClass MyCoordinates;

   int main (char[][] args)
   {
     // dont care about this in this example
     int ErrorCode = 0;

     // DoSomething();

     // dont care about this in this example
     return ErrorCode;
   } // int main()   

};

// this is done automatically:
example MyExample;
MyExample.main();

Cheers.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477040

Objects that are global variables (or more precisely variables "at namespace scope") have static storage duration. This means they live until the end of the program, and they are initialized during program startup (either during the static or the dynamic initialization phase).

The order of initialization is not generally specified except that all such global objects are initialized before main() is called, and that the initialization does not introduce data races.

(Common techniques to sequence mutually dependent global initialization is to replace naked global variables with a global getter function and a block-static variable:

Foo & getFoo() { static Foo impl; return impl; }

Now any other global using getFoo() in its own constructor will be initialized after impl.)

Upvotes: 1

Tom Kerr
Tom Kerr

Reputation: 10720

It is just a static object. It is treated like any other global variable.

It will not be tied to any stack frames and will be created when the anything in the file is loaded for the first time.

Generally, people will not recommend relying on globals from a design perspective. It depends though, they can be reasonable.

If you are doing any sort of threading they can be an issue. You also want to minimize different parts of your application knowing that things are global variables, it leads to a lot of spaghetti code.

If the variable is not referenced outside of the file, or for cross-cutting concerns, then sometimes it can be a good thing.

The best advice is to avoid it when you can, don't over design it when you can't.

Upvotes: 3

Related Questions