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?
Obviously,
this->getTBFInstallation()returns a const pointer. You need to make the functiongetTBFCmdHandlerconst as well.Note the
constkeyword at the end of the first line.Edit: By adding
const, you’re in effect changing the type ofthisfromTBFInstallation *toTBFInstallation const *. Basically, by adding theconst, you’re saying that the function can be called even when the object on which the function is being called is const.