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 meant by the term name mangling in c++?
Why is c++ awesome?
Which field is used in c++?
Do class declarations end with a semicolon? Do class method definitions?
Will a catch statement catch a derived exception if it is looking for the base class?
Do vectors start at 0?
What are put and get pointers?
What is split a string in c++?
What is the meaning of string in c++?
What is the difference between an enumeration and a set of pre-processor # defines?
What are the different types of comments allowed in c++?
What are compilers in c++?
What are enumerations?
How does atoi function work?
What is endl c++?