您的位置:首页 > 其它

volatile用法

2013-10-13 00:44 267 查看
1. volatile 修饰基础变量,意为该变量拒绝cache优化,每次存取直接访问内存;

2. volatile 修饰函数前,则意为无需为调用函数保存返回地址;

3. volatile 如果在后面同时该函数为类方法,那么当该类对应的实例对象为volatile时,该方法可以被调用,volatile对象是不能调用非volatile方法的,例如:

 

class A
{
volatile int m ;

public:
bool is_ok() volatile
{
if (m) return true;
else return false;
}

bool is_not_ok()
{
if (m) return false;
else return true;
}
};

int main()
{
volatile A obj;
obj.is_ok();//  it is ok
obj.is_not_ok();// it will have compile error "error: no matching function for call to 'A::is_not_ok() volatile'"
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: