开发者

What is the scope of these variables?

开发者 https://www.devze.com 2023-03-17 05:29 出处:网络
Please see the C++ code fragment below: #include ..... Class1 class1; Class2 class2; ... void Class3::foo() {

Please see the C++ code fragment below:

#include .....

Class1 class1;
Class2 class2;
...

void Class3::foo() {
    ...
}

What are the variables: class1 and class2? Are they global variables? Static variables? What actually are these? In C++ OO pro开发者_如何学Gogramming, is it good practice to use these because any member function of any class in the file can access them?

Sorry for the beginner's question.

Thanks.


Yes class1 & class2 are global variables.

What are global variables?
Variables declared outside of a block are called global variables. Global variables have program scope, which means they can be accessed everywhere in the program, and they are only destroyed when the program ends.

Because global variables have program scope, they can be used across multiple files. In order to use a global variable that has been declared in another file, you have to use a forward declaration or a header file, along with the extern keyword. Extern tells the compiler that you are not declaring a new variable, but instead referring to a variable declared elsewhere.

In C++ OO programming, is it good practice to use these because any member function of any class in the file can access them?

Typically, people use global variables because:

  • They don’t understand C++ variable passing mechanics, or they’re being lazy.
  • To hold data that needs to be used by the entire program (eg. configuration settings).
  • To pass data between code that doesn’t have a caller/callee relationship (eg. multi-threaded programs)

But Global variables are evil!!
Why?
For the simple reason that they increase the complexity of a program by manyfolds.
It is difficult to keep track of a global variable getting modified, simply because it can be modified anywhere across any of the multiple files.

In multithreaded programs, multiple threads can race to acquire these global variables, So these global' s should always be protected through some sort of an synchronization mechanism. Typically, it is hard to understand and write such mechanism unless you understand the entire system.

Since you asked,
What are Static variables?
static variables are variables which will be qualified by the keyword static.

How are static variables different from global variables?
An important differentiating point to be considered:

Scope:
Scope of the object is whether the object is visible(known by its name) at this position where it is being accessed..

Static variables are local to the block in which they are defined, while global variables are accessible across any file across the program.


These are global variables (objects in this case). A local variable or object would be declared inside of a method, function or class (basically inside of enclosing blocks) and will not be accessible outside of it. Generally, you should not use globals because like you said, anything in the same scope can have access to them. However, there are some few cases where you'll find that you may need them. Although if you really have to pass a variable around to a lot of functions in such a manner, you might be better of just passing it by reference.


class1 and class2 are global variables. Is it good practice? Probably not; you cannot rely on the initialisation order of such variables across translation units.

You can achieve a reliable order using the following idiom:

inline Class1& getClass1() {
  static Class1 object;
  return object;
}

Using this you know exactly when the Class1 object is going to be initialised (right before you need it).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号