c++ - What is the scope of these variables? -


please see c++ code fragment below:

#include .....  class1 class1; class2 class2; ...  void class3::foo() {     ... } 

what variables: class1 , class2? global variables? static variables? these? in c++ oo programming, practice use these because member function of class in file can access them?

sorry beginner's question.

thanks.

yes class1 & class2 global variables.

what global variables?
variables declared outside of block called global variables. global variables have program scope, means can accessed everywhere in program, , destroyed when program ends.

because global variables have program scope, can used across multiple files. in order use global variable has been declared in file, have use forward declaration or header file, along extern keyword. extern tells compiler not declaring new variable, instead referring variable declared elsewhere.

in c++ oo programming, practice use these because member function of class in 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 needs used entire program (eg. configuration settings).
  • to pass data between code doesn’t have caller/callee relationship (eg. multi-threaded programs)

but global variables evil!!
why?
simple reason increase complexity of program manyfolds.
difficult keep track of global variable getting modified, because can modified anywhere across of multiple files.

in multithreaded programs, multiple threads can race acquire these global variables, these global' s should protected through sort of synchronization mechanism. typically, hard understand , write such mechanism unless understand entire system.

since asked,
what static variables?
static variables variables qualified keyword static.

how static variables different global variables?
important differentiating point considered:

scope:
scope of object whether object visible(known name) @ position being accessed..

static variables local block in defined, while global variables accessible across file across program.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -