Tuesday, July 28, 2009

C++ question..more details inside?

What are the TWO BIG difference between a STATIC CLASS VARIABLE and a AUTO CLASS VARIABLE?

C++ question..more details inside?
class variables





Also known as static variables, these are any variables (properties) that are associated with a class, and not an object instance. There is always only one copy of a class variable, regardless of the number of object instances that are created from that class. Class variables do not use the prototype object to implement inheritance. You access a class variable directly through the class, and not through an object instance; you must define a class in a constructor function before you can define class variables.





Variable Storage Classes


Automatic: auto





storage is automatically allocated on function/block entry and automatically freed when the function/block is exited


may not be used with global variables (which have storage space that exists for the life of the program)


auto is the default for function/block variables


auto int a is the same as int a


because it is the default, it is almost never used








Static Storage: static





if used inside a block or function, the compiler will create space for the variable which lasts for the life of the program





int


counter(void)


{


static int cnt = 0;





return cnt++;


}








The following example defines two class variables--MAX_SPEED and MIN_SPEED.





function Car() { // define a Car class


...


}





Car.MAX_SPEED = 165;


Car.MIN_SPEED = 45;





You would access the MAX_SPEED and MIN_SPEED class variables directly from the Car class.





var carMaxSpeed = Car.MAX_SPEED; // carMaxSpeed = 165


var carMinSpeed = Car.MIN_SPEED; // carMinSpeed = 45


No comments:

Post a Comment