高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】oddbhostappservicesgetfontmapfilename90 cons
oddbhostappservices::getfontmapfilename() const
oddbhostappservices::getfontmapfilename() const
i inherited oddbhostappservices and overode getfontmapfilename()
i'm not able to use any non-const methods in my getfontmapfilename() which means i'm not able to to do much since most of the methods are non-const.
is there a reason why oddbhostappservices::getfontmapfilename() is const?
any suggestions?
thanks,
codey
hi,
and what methods for example do you want to call from your getfontmapfilename() ?
getfontmapfilename() intended to access a string, so it's const.
when it's reasonable, you can call non-const method using const_cast operator, e.g.:
odstring myhostappservices::getfontmapfilename() const
{
const_cast<myhostappservices*>(this)->nonconstmethod();
return m_sfontmapfilename;
}
quote:
originally posted by dmitry a. novikov
and what methods for example do you want to call from your getfontmapfilename() ?
the method can be any from our inherited class or from oddbhostappservices in which most are non-const. when getfontmapfilename() is called by the system, we wan to do more than just return a string. we may want to find it, add it to a list, etc...
anyway, thanks for your workaround, it works fine.
codey
what about declaring member variables as mutable. while i have not used that keyword myself, i have seen it used in c++ code. when i looked it up in msvc++ help, here is what it says:
mutable member-variable-declaration;
this keyword can only be applied to non-static and non-const data members of a class. if a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.
therefore, even if the member function is declared as const, as member functions in dwgdirect often are, it can still modify the mutable member data.
i don't know if this is a better solution than the const_cast or not. it seems better.
|