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
Do the names of parameters have to agree in the prototype, definition, and call to the function?
Explain friend class?
How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?
What are protected members in c++?
What are the advantages of using a pointer? Define the operators that can be used with a pointer.
What is conditions when using boolean operators?
What is the difference between structure and class?
Define vptr.
What do you mean by ‘void’ return type?
Which header file allows file i/o with streams a) fileio.h b) iostream.h c) fstream.h
What are disadvantages of pointers?
Difference between strdup and strcpy?
What is the use of setfill in c++?
How would you differentiate between a pre and post increment operators while overloading?
How does java differ from c and c++?