我有一些问题,消除常量性使用的const_cast。 错误味精说“转换为有效的标准转换......”
这是什么问题的本质是什么? 为什么要使用C样式转换呢?
““错误C2440:‘的const_cast’不能从‘常量为size_t’到‘为size_t’转换”的转换是一个有效的标准转换,其可以隐含地或通过使用的static_cast,C样式转换或函数样式转换来执行”
template<typename T>
const IFixedMemory* FixedMemoryPkt<T>::operator=(const IFixedMemory* srcObj)
{
// doesn't remove constness this way. why?
const_cast<char*> (this->m_Address) = (char*)srcObj->GetAddress();
// compile this way, but maybe(?) undefined behaviour
// const_cast<char*&> (this->m_Address) = (char*)srcObj->GetAddress();
// doesn't doesn't work too
const_cast<size_t> (this->m_Size) = (size_t)(srcObj->GetSize());
// const_cast<size_t> (this->m_Size) = 0;
return this;
}
template<typename T>
class FixedMemoryPkt : public IFixedMemory
{
private:
const size_t m_Size;
const char* m_Address;
}
class IFixedMemory
{
public:
virtual const char* GetAddress() const = 0;
virtual size_t GetSize() const = 0;
}