拥有包含这些语句的文件:

public:
boost::shared_ptr<TBFControl::TbfCmdHandler> _tbfCmdHandlerPtr;
// will be private later...

boost::shared_ptr<TBFControl::TbfCmdHandler> getTBFCmdHandler()
{ return _tbfCmdHandlerPtr; }

我可以这样使用:

boost::shared_ptr<TBFControl::TbfCmdHandler>myTbfCmdHandlerPtr(
    this->getTBFInstallation()-> _tbfCmdHandlerPtr );

但是不是像我想要的那样,这样:

boost::shared_ptr<TBFControl::TbfCmdHandler>myTbfCmdHandlerPtr(
    this->getTBFInstallation()->getTBFCmdHandler() );

使用Getter函数,发生以下错误:

'家政:: tbfinstallation :: getTbfcmdhandler':无法将“ const家居待办事项:: tbfinstallation”转换为“ tbfinstallation''到“家政务:: tbfinstallation&''

这里出了什么问题?

有帮助吗?

解决方案

明显地, this->getTBFInstallation() 返回const指针。您需要制作功能 getTBFCmdHandler const。

boost::shared_ptr<TBFControl::TbfCmdHandler> getTBFCmdHandler() const
{
    return _tbfCmdHandlerPtr;
}

注意 const 关键字在第一行的末尾。

编辑: 通过添加 const, ,您实际上改变了 thisTBFInstallation *TBFInstallation const *. 。基本上,通过添加 const, ,您说的是,即使称为函数的对象为const,函数也可以被调用。

其他提示

getTBFInstallation() 是(显然)返回const指针。然而, getTBFCmdHandler() 是非CONST成员函数,因此无法在const指针上调用。解决方案:制作 getTBFCmdHandler() const成员函数

没有看过代码,我想 getTBFInstallation() 返回a const TBFInstallation 您试图调用非const函数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top