我正在尝试使用内存地址或指针类型作为键和值,但由于某种原因,我在插入时遇到访问冲突。
class SomeClass
{
public:
std::map<MyClass*, MyClass*> stlMapPointer;
std::map<size_t, size_t> stlMapAddress;
CMap<MyClass*, MyClass*, MyClass*, MyClass*> mfcMapPointer;
CMap<size_t, size_t, size_t, size_t> mfcMapAddress;
}
SomeOtherClass
{
public:
SomeClass *m_someClassRef;
void SomeOtherClass::some_method(MyClass* ptr, ...);
}
void SomeOtherClass::some_method(MyClass* ptr, ...)
{
MyClass* test = ptr;
size_t address = reinterpret_cast<size_t>(test); // I realize size_t is technically not portable
// PROBLEM STARTS HERE: Every single one of the following insertions will yield an access violation while calling various internal CMap/std::map calls.
m_someClassRef->stlMapPointer.insert(std::pair<MyClass*, MyClass*>(test, test));
m_someClassRef->stlMapPointer.insert(std::pair<size_t, size_t>(address, address));
m_someClassRef->mfcMapPointer.SetAt(test, test);
m_someClassRef->mfcMapAddress.SetAt(address, address);
}
MyClass 没有复制构造函数,但我认为这无关紧要。如果这完全相关,我也坚持使用 Visual Studio 6。
关于为什么会发生这种情况的任何想法?
谢谢!