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


Please Help Members By Posting Answers For Below Questions

What is meant by the term name mangling in c++?

514


Why is c++ awesome?

565


Which field is used in c++?

628


Do class declarations end with a semicolon? Do class method definitions?

610


Will a catch statement catch a derived exception if it is looking for the base class?

554






Do vectors start at 0?

588


What are put and get pointers?

577


What is split a string in c++?

686


What is the meaning of string in c++?

566


What is the difference between an enumeration and a set of pre-processor # defines?

824


What are the different types of comments allowed in c++?

569


What are compilers in c++?

598


What are enumerations?

650


How does atoi function work?

617


What is endl c++?

598