Volatile function

自作多情 提交于 2019-12-03 10:23:58

In your code, the volatile keyword does not apply to the function, but to the return type, it is the equivalent of:

typedef volatile int Type;
Type foo();

Now, in C++ you can make a member function volatile, in the same way that the const qualifier, and the behavior is the same:

struct test {
   void vfunction() volatile;
};

Basically you cannot call a non-volatile (alterantively non-const) function on a volatile (const respectively) instance of the type:

struct test {
   void vfunction() volatile;
   void function();
};
volatile test t;
t.vfunction();      // ok
t.function();       // error

foo() is not volatile.

It's a function that returns a volatile int.

Which is legal. But strange for a returned int.

Member functions, on the other hand, can be volatile for the same reason they can be const -- both describe the object this is pointing to.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!