Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-static vs. static function and variable

Tags:

c++

I have one question about static and non-static function and variable.

1) non-static function access static variable.

It's OK!

class Bar
{
public:

     static int i;

     void nonStaticFunction() {

         Bar::i = 10;

     }

};

int Bar::i=0;

2) non-static function access non-static variable

Definitely OK!

3) static function access static variable&funciton

Definitely OK!

4) static function access non-static function

It's OK

class Bar
{
public:
     static void staticFunction( const Bar & bar)
     {
         bar.memberFunction();

     }

     void memberFunction() const
     {

     }

}

5) static function access non-static variable

It's OK or not OK? I am puzzled about this!

How about this example

class Bar
{
public:
     static void staticFunction( Bar & bar)
     {
         bar.memberFunction();

     }

     void memberFunction()
     {

         i = 0;
     }

     int i;

};
like image 734
skydoor Avatar asked Feb 18 '10 02:02

skydoor


People also ask

What is the difference between static variable and non static variable?

A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once. A non-static variable may occupy more space.

What is static and non static function?

The static method uses compile-time or early binding. The non-static method uses runtime or dynamic binding. Overriding. The static method cannot be overridden because of early binding. The non-static method can be overridden because of runtime binding.

What is the difference between static and non static members?

static members are accessed by their class name which encapsulates them, but non-static members are accessed by object reference. static members can't use non-static methods without instantiating an object, but non-static members can use static members directly.


2 Answers

For this, you need to understand what is static.

Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. They will have a class scope and does not bound to an instance of the class.

To access static member of the class, we use the format as below ::

if you have created 10 objects of a class. Assume, you were able to access the non-static variable in the static member of the class, When the static function is called, which object's member it needs to change?

like image 191
Narendra N Avatar answered Oct 19 '22 23:10

Narendra N


It's not ok. Static functions are accessible without having an instance of a class and thus can't access information that you would need an instance to determine.

For example, you don't need a car to know how many wheels it has, blueprints for a general car would suffice (that could be static information) but you can't tell what color the car is unless you're referring to a specific car (that information needs a specific instance of an object.)

like image 42
Pace Avatar answered Oct 20 '22 00:10

Pace