几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   DirectDWG (http://www.dimcax.com/hust/forumdisplay.php?f=89)
-   -   【转帖】oddbhostappservicesgetfontmapfilename90 cons (http://www.dimcax.com/hust/showthread.php?t=17543)

yang686526 2009-05-06 07:49 PM

【转帖】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.


所有的时间均为北京时间。 现在的时间是 04:08 AM.