By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

static member

P: n/a
It's my understanding a static member variable will be one instance among
all objects created from it, and static member functions only access static
variables. When coding, I am still not sure how to decide if to declare a
member of a class "static"? Is there some tips on this? thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ian
Lowen wrote:
It's my understanding a static member variable will be one instance among
all objects created from it, and static member functions only access static
variables. When coding, I am still not sure how to decide if to declare a
member of a class "static"? Is there some tips on this? thanks!

I think you have answered your own question!

If the variable is pertinent to the current instance of the class, it a
normal member, if it its value is shared by all instances of the class,
it is static.

Ian

Jul 22 '05 #2

P: n/a
Ian posted:
Lowen wrote:
It's my understanding a static member variable will be one instance
among all objects created from it, and static member functions only
access static variables. When coding, I am still not sure how to
decide if to declare a member of a class "static"? Is there some tips
on this? thanks!

I think you have answered your own question!

If the variable is pertinent to the current instance of the class, it a
normal member, if it its value is shared by all instances of the class,
it is static.

Ian

Every dog has a unique name, every dog has a unique age. But all dog's are
of the same order "Mammal", so this is not a unique bit of info for each
individual dog:

class Dog
{
public:
char Name[15];
unsigned short int Age;

static char Order[15];
};

char Dog::Order[15] = "Mammal";
-JKop
Jul 22 '05 #3

P: n/a
"Lowen" <Lo****@import.com> wrote in message news:<bh******************@bgtnsc05-news.ops.worldnet.att.net>...
It's my understanding a static member variable will be one instance among
all objects created from it, and static member functions only access static
variables. When coding, I am still not sure how to decide if to declare a
member of a class "static"? Is there some tips on this? thanks!


One of my instructors had this "trick" for deciding when to use
static and when to use non-static.

Think of the class definition as a factory, and the objects (the
instances of the class) as the cars produced in that factory.

If the information is about the car (the instance) then it should
be "per car" and a non-static member.

If the information is about the factory (the class) and not about
individual cars, then it should be static.

So, to say it another way, the static members should be information
that applies to the entire set of objects, no matter how many there
are, and the non-static should be information about inidividual
objects.

So, for example, a count of cars the factory had produced so far
would be static. The mileage on an individual car would be non-static.
Socks
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.