我有一个 const 成员函数 bar
我想使用 this
指针来调用 ClFoo
基类的函数.
虽然我得到一个编译器错误,说:
'ClRDFoo::ReadCounterfile' : cannot convert 'this' pointer from 'const ClFoo' to 'ClRDLFoo &'
这些是方法和类:
//ClFoo.cpp
int ClFoo::bar( void ) const
{
int nCounter = 0;
this->ReadCounterFile(&nCounter);
}
//ClFoo.h
class ClFoo : public ClRDFoo
{
protected:
int ClFoo::bar( void ) const;
}
//ClRDFoo.h
class ClRDFoo
{
protected:
virtual bool ReadCounterFile(void *pBuffer);
}
最佳答案
您正在尝试从常量函数 (void bar() const
) 调用非常量成员函数 (bool ReadCounterFile(void*)
)。这破坏了 const 的正确性,是不允许的。
您必须使 ReadCounterFile
const
或使 bar()
成为非 const。
关于c++ - 在 const 成员函数中使用 this 指针 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919005/