Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 the basic structure of a c++ program?

1144


what is a class? Explain with an example.

1137


Define virtual constructor.

1094


How to tokenize a string in c++?

1056


When should overload new operator on a global basis or a class basis?

1077


What is the difference between object-oriented programming and procedural programming?

1231


Explain the differences between private, public and protected and give examples.

1023


Describe the advantages of operator overloading?

1040


What is DlgProc?

1024


What is the size of a vector?

1135


Why pointer is used in c++?

1157


Explain how to initialize a const data member.

1123


How do c++ struct differs from the c++ class?

1192


What are function prototypes?

1238


What is the need of a destructor? Explain with the help of an example.

1026