Difference between static global and global?
Answer Posted / saugat biswas
I think all the answers above are restricted to C only.
However if extended to C++, static has a file scope and the
static variable is a variable for the class and not the
instances. In other words, static variables are shared
variables. All the instances of the class actualy use the
same static variable. Static variables and function can
directly be called without creating instances of the class
by using scope resolution operator. Static variables are
not initialized in constructors rather they are initialized
as global variables. Example
Example.h
~~~~~~~~~
class A
{
public:
static int x;
A();
~A();
static int getX(void);
}
Example.cpp
~~~~~~~~~~~
int x = 0;
A() //Contructor
{}
~A() //Destructor
{}
int getX(void)
{
return x;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now in class B we can write:
#include "Example.h"
class B
{
int myX = Example::getX();
}
| Is This Answer Correct ? | 18 Yes | 0 No |
Post New Answer View All Answers
What is the basic structure of a c++ program?
what is a class? Explain with an example.
Define virtual constructor.
How to tokenize a string in c++?
When should overload new operator on a global basis or a class basis?
What is the difference between object-oriented programming and procedural programming?
Explain the differences between private, public and protected and give examples.
Describe the advantages of operator overloading?
What is DlgProc?
What is the size of a vector?
Why pointer is used in c++?
Explain how to initialize a const data member.
How do c++ struct differs from the c++ class?
What are function prototypes?
What is the need of a destructor? Explain with the help of an example.