I would like to ask if using mutable is appropriate:
#include <iostream> class Base { protected: int x; public: virtual void NoMod() const { std::cout << x << std::endl; } void Draw() const { this->NoMod(); } }; class Derive : public Base { private: mutable int y; public: void NoMod() const { y = 5; } }; int main() { Derive derive; // Test virtual with derive derive.Draw(); return 0; }
The base class is a third-party library. I am expanding it to provide my own NoMod (). The original NoMod () library is declared as const.
My NoMod () differs from Base in that it needs to change its own member variable.
So for my own NoMod () to compile and call the call when Draw () is called, I had to
1) Implement Derive :: NoMod () as const 2) make my int y mutable.
Is this the best I can do?
As described in the geek chapter, the answer to your question depends on how your data member is used.
.
"" "" . .
, "" , " /", . , , . .
, , , . , . , , , . , . . booean data, , , , "" .
, const, . , , , , , , ( , ). , . , const, .
, , , , , , .
, - , y .
y
mutable , "" . , C, mLength, , , const. , mutable .
mutable
mLength
const
, , mutable , , , .
y , , , .
, "", - - "const", (=), 'const'.
, const_cast apllied 'const' U.B. ++. , "const", , "" .
, ! .
,
mutable, (, /, ).
. const . const_cast:
const_cast< Derive*>( this)->y = 10;
const_cast, , , const, , .
, , , , mutable .
, , :
, , mutable , ++.
, , , . ++ - , , " ".
, , , NoMod , const. , . NoMod , , const .
, .
Source: https://habr.com/ru/post/1706428/More articles:Printing CrystalReport reports in Asp.Net without using PDF - asp.netTesting models with multiple database connections in Rails using ActiveRecord - ruby | fooobar.comAccessing Views Created in Interface Builder - objective-cHow to send custom event from actioncript 3 class and listen to it in document root directory? - eventsStarting Windows service in console - c #MSChart and ASP.NET MVC Partial View - asp.netSubtype Base Tables - sqlis there a statically typed language with good lambda support for the JVM? - jvm-languages | fooobar.comКаковы хорошие SQL Server 2008 с поставщиками хостинга веб-сайтов ASP.NET? - sql-serverC / C ++ Console Windows WIN32 - c ++All Articles