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

Do the names of parameters have to agree in the prototype, definition, and call to the function?

1031


Explain friend class?

1138


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?

1291


What are protected members in c++?

1138


What are the advantages of using a pointer? Define the operators that can be used with a pointer.

1037


What is conditions when using boolean operators?

1241


What is the difference between structure and class?

1287


Define vptr.

1039


What do you mean by ‘void’ return type?

1072


Which header file allows file i/o with streams a) fileio.h b) iostream.h c) fstream.h

1136


What are disadvantages of pointers?

1042


Difference between strdup and strcpy?

1210


What is the use of setfill in c++?

1080


How would you differentiate between a pre and post increment operators while overloading?

1156


How does java differ from c and c++?

1007