简体   繁体   中英

what can you initialize global variables to in c++

Can you initialize global variables in c++, and if possible, what values are allowed?

The allowed values depend on the type of the variable. For an int , 0 or 42 are fine. For a string , "hello world" works. Initializing globals is identical to initializing other variables, so you have a lot of freedom with what you can use.

It's typically best to keep it simple; if you need a complex initializer for a global, the variable may be in the wrong spot, but you can do quite a bit if need be.

The syntax for doing so, at its most basic, is:

int global_Int = 42;
string global_String = "Hello World";

However, if this is done in a header (since globals are typically declared in a common header), you will get duplicate declarations and errors. To solve that, use the extern keyword to declare them, then initialize in a code file:

Header:

extern int global_Int;
extern string global_String;

Code:

#include "header.hpp"

int global_Int = 42;
string global_String = "Hello World";

Then just include your header whenever you need to use the variables. You can also add other keywords as needed (if you need const globals, for example).

Yes, of course you can initialize globals. For built-in types, you can use any value you could use for assignment (and you can also initialize arrays, which you can't assign). For a class type, it's up to you to decide what types/values will be allowed (and/or required) when you design the class. For a pre-defined class, you basically have to consult the documentation to figure out what it allows/requires.

Ultimately, however, the fact that it's a global doesn't affect the values that can be supplied. Being a global can affect how you can get the value though--in particular, you obviously can't use values that aren't available yet when that initialization is done. This can be a bit more subtle than it initially seems because very little is guaranteed about the order in which global variables are initialized compared to each other, so (for example) when the constructor for your global variable executes, std::cin , std::cout and std::cerr may not have been constructed yet, so if you want to initialize something based on input from the user, you may have to take special steps to either ensure the order of initialization or else (for example) use C-style I/O for that particular job.

Of course we can. Why can't we?

//global variables
int g_int = 100;
int g_float = 10.0;
int g_char = 'A';

class X 
{
   int value;
   public:
      X(int v) : value(v){}
};

X g_x1(10);  //initialize with argument 10
X g_x2 = X(198); //with arg 198

int main()
{
}

Could be pretty much anything, as in constructor call:

static const std::string boo( "42" );

Be careful though - these run before main() but their order is undefined (see "static initialization order fiasco" ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM