non-static vs. static function and variable

前端 未结 4 1589
無奈伤痛
無奈伤痛 2021-02-04 12:55

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

1) non-static function access static variable.

It\'s OK!

class Bar
{
publ         


        
4条回答
  •  天命终不由人
    2021-02-04 13:57

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

提交回复
热议问题