开发者

non-static vs. static function and variable

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;

};


static function access non-static variable

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

When called, a static function isn't bound to an instance of the class. Class instances (objects) are going to be the entities that hold the "non-static" variables. Therefore, from the static function, you won't be able to access them without actually being passed or storing elsewhere a specific instance to operate on.

So yes, the code in your last example is valid, because you are passed in an instance. However, you could not do:

static void staticFunction()
{
    // error, this function is static, and is therefore
    // not bound to a specific instance when called
    i = 5;


}


Static means this is independent of a particular instance of the class. Static methods don't have access to the this pointer. That is the reason you need to call them using the class name.

When you call the Static method, you might not even have any instance of the class defined.

non-static means implies an instance, and could be different with different instances.

So, basically, it does not make sense to access non-static members from static methods.


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?


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜