C++ 这个代码有什么问题?我必须使用一个指向union的易失性指针,这会导致运算符重载错误=
constvolatile.cpp:91:9:错误:将-volatile GENERIC_COMMAND–作为-const GENERIC_COMMAND和GENERIC_COMMAND::operator=(const T&)[with T=COMPLETION_WAIT,GENERIC_COMMAND=GENERIC_COMMAND]的-this–参数传递-丢弃限定符[-fppermissive]C++ 这个代码有什么问题?我必须使用一个指向union的易失性指针,这会导致运算符重载错误=,c++,operator-overloading,C++,Operator Overloading,constvolatile.cpp:91:9:错误:将-volatile GENERIC_COMMAND–作为-const GENERIC_COMMAND和GENERIC_COMMAND::operator=(const T&)[with T=COMPLETION_WAIT,GENERIC_COMMAND=GENERIC_COMMAND]的-this–参数传递-丢弃限定符[-fppermissive] #include <iostream>
#include <iostream>
using namespace std;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
union GENERIC_COMMAND
{
struct
{
uint64_t first_operand : 60;
uint64_t opcode : 4;
uint64_t second_operand : 64;
};
struct
{
uint32_t dword1;
uint32_t dword2;
uint32_t dword3;
uint32_t dword4;
};
struct
{
uint64_t qword1;
uint64_t qword2;
};
GENERIC_COMMAND() {
}
GENERIC_COMMAND(volatile const GENERIC_COMMAND&){}
template <typename T>
volatile GENERIC_COMMAND& operator=(const T& rhs) volatile
{
this->dword1 = rhs.dword1;
this->dword2 = rhs.dword2;
this->dword3 = rhs.dword3;
this->dword4 = rhs.dword4;
return *this;
}
};
union COMPLETION_WAIT
{
struct
{
uint64_t s : 1;
uint64_t i : 1;
uint64_t f : 1;
uint64_t store_address : 49;
uint64_t reserved1 : 8;
uint64_t opcode : 4;
uint64_t store_data : 64;
};
struct
{
uint32_t dword1;
uint32_t dword2;
uint32_t dword3;
uint32_t dword4;
};
};
void add_completion_wait_command(uint32_t s, uint32_t i, uint32_t f,
uint64_t store_address, uint64_t store_data,
bool auto_flush)
{
COMPLETION_WAIT command;
command.dword1 = 0;
command.dword2 = 0;
command.dword3 = 0;
command.dword4 = 0;
command.s = s;
command.i = i;
command.f = f;
command.store_address = store_address >> 3;
command.opcode = 0x1;
command.store_data = store_data;
GENERIC_COMMAND generic;
static_cast<GENERIC_COMMAND>(generic = command);
}
main()
{
cout<< "in main"<< endl;
volatile GENERIC_COMMAND* A;//this has to be volatile only.
COMPLETION_WAIT cw;
A = new volatile union GENERIC_COMMAND;
static_cast<GENERIC_COMMAND>(A[0] = cw);
}
#包括
使用名称空间std;
typedef unsigned int uint32\u t;
typedef无符号长uint64\u t;
联合通用命令
{
结构
{
uint64第一个操作数:60;
uint64操作码:4;
uint64第二个操作数:64;
};
结构
{
uint32_t dword1;
uint32_t dword2;
uint32_t dword3;
uint32_t dword4;
};
结构
{
uint64_t qword1;
uint64_t qword2;
};
通用命令(){
}
通用命令(volatile const通用命令&){
模板
volatile泛型命令和运算符=(常量T和rhs)volatile
{
此->dword1=rhs.dword1;
此->dword2=rhs.dword2;
此->dword3=rhs.dword3;
此->dword4=rhs.dword4;
归还*这个;
}
};
联盟完成了吗
{
结构
{
uint64_t s:1;
uint64_t i:1;
uint64_t f:1;
uint64存储地址:49;
uint64_t储备1:8;
uint64操作码:4;
uint64存储数据:64;
};
结构
{
uint32_t dword1;
uint32_t dword2;
uint32_t dword3;
uint32_t dword4;
};
};
void add_completion_wait_命令(uint32_t s、uint32_t i、uint32_t f、,
uint64存储地址,uint64存储数据,
bool(自动冲洗)
{
完成等待命令;
command.dword1=0;
command.dword2=0;
command.dword3=0;
command.dword4=0;
command.s=s;
command.i=i;
命令f=f;
command.store\u address=store\u address>>3;
command.opcode=0x1;
command.store_data=store_data;
通用命令;
静态_cast(通用=命令);
}
main()
{
coutYour operator=需要是volatile。正如在const对象上调用的成员函数需要是const一样。如果您的成员函数不是volatile,编译器将优化其主体,这不是volatile对象想要的。因此,该语言有这个有用的规则
众所周知,即使使用隐式声明的运算符=,也会发生此错误,并且记录为与C语言不兼容的原因之一
编辑:我想指出的是,您分配给的对象不是volatile,因此编译器可以自由地优化对它的操作,即使用于访问它的指针具有volatile限定符。将volatile添加到它的类型中
new volatile union GENERIC_COMMAND
运算符=需要是易失性的。正如成员函数需要是常量才能在常量对象上调用一样。如果成员函数不是易失性的,编译器将优化其主体,这不是易失性对象所需要的。因此,该语言具有此有用规则
众所周知,即使使用隐式声明的运算符=,也会发生此错误,并且记录为与C语言不兼容的原因之一
编辑:我想指出的是,您分配给的对象不是volatile,因此编译器可以自由地优化对它的操作,即使用于访问它的指针具有volatile限定符。将volatile添加到它的类型中
new volatile union GENERIC_COMMAND
需要澄清的是:你说你有一个“volatile指针”,但在你的代码中你有一个指向volatile对象的指针。你想要哪一个?@jrok是的,它是指向volatile对象的指针。需要澄清的是:你说你有一个“volatile指针”,但在代码中有一个指向易失性对象的指针。您想要哪一个?@jrok yes它是指向易失性对象的指针。making operator=volatile不起作用。代码中的声明使[0]易失性(对象),构造函数中的const是对对象的引用。无法解析“警告:隐式取消引用将无法使用静态\u强制转换和复制构造函数访问语句”中类型为volatile GENERIC \u COMMAND的对象。making operator=volatile不起作用。代码中的声明使[0]volatile(object)成为volatile,而构造函数中的const是对object的引用。无法解析”警告:隐式取消引用将无法访问“static_cast and copy构造函数”语句中volatile GENERIC_COMMAND类型的对象。