473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

difference between static and ordinary member variables

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

thankyou in advance,
vishnu
Jul 22 '05 #1
3 2745

"prabhu" <gp*********@yahoo.co.in> wrote in message
news:23**************************@posting.google.c om...
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
Jul 22 '05 #2
Hi!

prabhu wrote:
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

Jul 22 '05 #3
gp*********@yahoo.co.in (prabhu) writes:
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;
}
--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
5
by: Surya Kiran | last post by:
Hi all, I've 2 classes class A, and class B. and i want to use list of type B in class A. like this list<B> typeB ; till now its fine. But i want to make it a static member. like static...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.