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
Explain the difference between abstract class and interface in c++?
What is a hashmap c++?
Is map ordered c++?
What is #include cmath?
What are friend classes? What are advantages of using friend classes?
How would you implement a substr() function that extracts a sub string from a given string?
What is difference between c++ 11 and c++ 14?
Define 'std'.
Explain the volatile and mutable keywords.
give me an example for testing a program showing the test path .show how the test is important and complex.
Write syntax to define friend functions in C++.
Is c++ a low level language?
What is the purpose of template?
What is the most common mistake on c++ and oo projects?
Define whitespace in C++.