Question

having a file containing these statements:

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

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

I can use it this way:

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

but not, like i want, this way:

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

Using the getter function, the following error occurs:

'Housekeeping::TBFInstallation::getTBFCmdHandler' : cannot convert 'this' pointer from 'const Housekeeping::TBFInstallation' to 'Housekeeping::TBFInstallation &'

what is going wrong here?

Was it helpful?

Solution

Obviously, this->getTBFInstallation() returns a const pointer. You need to make the function getTBFCmdHandler const as well.

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

Note the const keyword at the end of the first line.

Edit: By adding const, you're in effect changing the type of this from TBFInstallation * to TBFInstallation const *. Basically, by adding the const, you're saying that the function can be called even when the object on which the function is being called is const.

OTHER TIPS

getTBFInstallation() is (apparently) returning a const pointer. However, getTBFCmdHandler() is a non-const member function and can therefore not be called on a const pointer. Solution: Make getTBFCmdHandler() a const member function

Without having seen the code I would guess that getTBFInstallation() returns a const TBFInstallation on which you're trying to call a non const function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top