繁体   English   中英

在const成员函数(C ++)中使用此指针

[英]Using this pointer in const member function (C++)

我有一个const成员函数bar ,我想使用this指针调用ClFoo基类的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);

    }

您正在尝试从一个const( void bar() const )调用一个非const成员函数( bool ReadCounterFile(void*) )。 这会破坏const的正确性,因此是不允许的。

您必须将ReadCounterFile const或将bar()为非const。

因为bar被标记为const ,所以它所能做的就是调用标记为const其他函数。 这是为了确保您不进行任何修改。

从作为常量的bar()函数中,您正在调用非常量函数ReadCounterFile(),这是不允许的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM