difference between static and ordinary member variables

P

prabhu

hello to all,
Please can anybody tell me the differnece between static and ordinary
member variables.

thankyou in advance,
vishnu
 
S

Sharad Kala

prabhu said:
hello to all,
Please can anybody tell me the differnece between static and ordinary
member variables.

It's better to look for answers to such questions in your favourite textbook
else google is your friend.
People on this ng expect the poster to have done some kind of homework before
asking questions.
Please take care of it.
Anyways, static members are common across all objects of a class i.e. there is
only one common instance for all objects of the class.
Member variables on the contrary are specific to each object and are not shared
amongst objects of the class.

Best wishes,
Sharad
 
M

Marco Wahl

Hi!
hello to all,
Please can anybody tell me the differnece between static and ordinary
member variables.

A static variable (lets say s) is one for a class (C). So in this case
each instance of class C has access to the same variable s (more exact
C::s).
A non-static variable (v) is one for each instance (i) of a class. So
each instance has its own v (more exact v == i.v).


Ciao, Marco
 
R

Roger Leigh

Please can anybody tell me the differnece between static and ordinary
member variables.

static variables are the opposite of automatic ("ordinary")
variables. When a function goes out of scope, the automatic variables
will be destroyed, but the static variables will remain.

Try this example:


#include <iostream>

class test
{
public:
void print()
{
auto int i = 2;
static int j = 2;

std::cout << "i = " << i << ", j = " << j << "\n";

++i;
++j;
}
};

int main()
{
test t;

for (int i=0; i<10; ++i)
t.print();

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top