我試圖常量性添加到C++中的變量,通過VC拒絕與Error C2664 : cannot convert MyClass * to const MyClass &
編譯。我已經竭盡所能,做搜索,閱讀類似的問題(1,2),我仍然解決不了。無法轉換MyClass的*爲const MyClass的
我的函數定義爲:
void ClassFoo::FuncFoo(MyClass* instance){
Merge(instance); // <--- Error C2664 -- cannot convert MyClass* to const MyClass &
Merge(&instance); // <--- Error C2664 -- cannot convert MyClass** to const MyClass &
Merge(*instance); // <--- This compiles fine, but doesn't work properly at runtime
Merge(const_cast<const GFxTextFormat&>(instance)); // <--- Error C2440
}
MyClass Merge (const MyClass &instance){
}
我應該怎麼做才能正常添加常量性的變量instance
,這樣我可以正確地調用Merge
呢?
,編譯精細的一個是正確的。運行時出現了什麼問題? – 2013-03-23 19:10:19
閱讀關於const_cast <> http://www.cplusplus.com/doc/tutorial/typecasting/ – OldProgrammer 2013-03-23 19:11:36