Volatile function

前端 未结 2 862
粉色の甜心
粉色の甜心 2021-02-13 01:36

Summary: What does the keyword volatile do when applied to a function declaration in C and in C++?

Details:

I se

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-13 02:25

    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
    

提交回复
热议问题