const_castを使用してconstnessを削除する際に問題が発生しました。エラーメッセージには、「変換は有効な標準変換です.....」と表示されます。
この問題の性質は何ですか?代わりにCスタイルのキャストを使用する必要があるのはなぜですか?
"エラーC2440:'const_cast':'constsize_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;
}