C 関数を呼び出している場所から、C++ に const 関数があります。
class ClassEx
{
A* pPointer // declaration of the pointer
};
void
ClassEx::ClassFunction() const
{
int error = AFunctionInExternLib(&pPointer); //pPointer will be instantiated by this function.
}
//Signature of AFunctionInExternLib
Struct A
{
};
AFunctionInExternLib(A** pPointer);
これで、構造体 A 型の classEx のメンバ変数ができました。Class::ClassFunction() は const 関数であるため、pPointer をそのまま渡すことはできません。だから私は次のように宣言しました
class ClassEx
{
mutable A* pPointer // declaration of the pointer
};
これは問題なくコンパイルされますが、変更可能なキーワードを使用せずにこれを達成する他の方法があるかどうか疑問に思っていましたか?
私もこれを試したことに注意してください、
void
ClassEx::ClassFunction() const
{
A* pAnotherPointer = const_cast<A*>(pPointer);// remove constness
int error = AFunctionInExternLib(&pAnotherPointer);
}
しかし、これは pPointer ではなく、pAnotherPointer をインスタンス化します。pAnotherPointerのアドレスをpPointerに共有する方法はありますか?
このアプローチに問題はありますか。
class ClassEx
{
A* pPointer // declaration of the pointer
};
void
ClassEx::ClassFunction() const
{
ClassEx* pTempPointer = const_cast<ClassEx*>(this);
int error = AFunctionInExternLib(&pTempPointer->pPointer);
}