C++ 对 volatile 对象的引用 - 原因和影响
C++ reference to volatile object - reasons and effects
我正在处理具有 volatile
对象引用的现有 C++ 代码
volatile vClass & vobj;
我来自C
所以我很熟悉volatile
当我们像这样访问内存映射IO时:
*((volatile unsigned int *) 0x12345678) = 0x5
问题
将 volatile
应用于(对)对象的影响是什么?
我猜想它的所有数据成员都继承了 volatile
但如果成员函数具有像
这样的非易失性内存访问呢?
void vClass::AccessMem() {*((unsigned int *) 0x12345678) = 0x5;}
内存访问也会变得不稳定吗?
它是对 volatile 的引用,而不是 volatile 引用(后者无论如何都没有意义)。
类似于:
volatile char * ptr = ...;
ptr 的内存可能会更改,恕不另行通知,但 ptr 本身是稳定的(不像
char * volatile ptr = ...;
成员函数必须是 volatile 限定的才能从 volatile 对象调用:
int not_a_member;
struct vClass {
int i;
void set(int j) {
i=j; //i is not accessed as a volatile
static_assert(std::is_same_v<decltype((i)),int&>);
}
void set_v(int j) volatile{
i=j; //here i is accessed as a volatile
static_assert(std::is_same_v<decltype((i)),volatile int&>);
not_a_member=j;//here not a volatile access because "not_a_member" is
// not a member.
//To understand what happen, since you are a C programmer it is going to be simple.
//The volatile qualifier is actualy apply to the "this" pointer,
// which is a pointer to the object from which this member function is
// called. So inside this function "this" as the type "volatile vClass"
//Inside a member function, all access to member data, as "i" are
//actualy short ends for "this->i". So in this access, "i"
//adopt the volatile qualifier of "this".
//So the volatile qualifier just applies to the data member of the object.
}
}
void use_vClass(){
volatile vClass x;
x.set(10); //Do not compile: "Error:try to access volatile object as non volatile"
x.set_v(10); //Compile
}
因此,由于从易失性对象或引用,您可以只调用易失性限定成员函数,所有数据成员访问都是 "volatile"。
我正在处理具有 volatile
对象引用的现有 C++ 代码
volatile vClass & vobj;
我来自C
所以我很熟悉volatile
当我们像这样访问内存映射IO时:
*((volatile unsigned int *) 0x12345678) = 0x5
问题
将 volatile
应用于(对)对象的影响是什么?
我猜想它的所有数据成员都继承了 volatile
但如果成员函数具有像
void vClass::AccessMem() {*((unsigned int *) 0x12345678) = 0x5;}
内存访问也会变得不稳定吗?
它是对 volatile 的引用,而不是 volatile 引用(后者无论如何都没有意义)。
类似于:
volatile char * ptr = ...;
ptr 的内存可能会更改,恕不另行通知,但 ptr 本身是稳定的(不像
char * volatile ptr = ...;
成员函数必须是 volatile 限定的才能从 volatile 对象调用:
int not_a_member;
struct vClass {
int i;
void set(int j) {
i=j; //i is not accessed as a volatile
static_assert(std::is_same_v<decltype((i)),int&>);
}
void set_v(int j) volatile{
i=j; //here i is accessed as a volatile
static_assert(std::is_same_v<decltype((i)),volatile int&>);
not_a_member=j;//here not a volatile access because "not_a_member" is
// not a member.
//To understand what happen, since you are a C programmer it is going to be simple.
//The volatile qualifier is actualy apply to the "this" pointer,
// which is a pointer to the object from which this member function is
// called. So inside this function "this" as the type "volatile vClass"
//Inside a member function, all access to member data, as "i" are
//actualy short ends for "this->i". So in this access, "i"
//adopt the volatile qualifier of "this".
//So the volatile qualifier just applies to the data member of the object.
}
}
void use_vClass(){
volatile vClass x;
x.set(10); //Do not compile: "Error:try to access volatile object as non volatile"
x.set_v(10); //Compile
}
因此,由于从易失性对象或引用,您可以只调用易失性限定成员函数,所有数据成员访问都是 "volatile"。