#include <iostream> #include <mutex> class Aa { public: virtual void printTest() const = 0; void printTest2() const { printTest(); std::cout << "Test 2" << std::endl; } }; class Bb : public Aa { private: std::mutex mMutex; public: virtual void printTest() const { mMutex.lock(); std::cout << "Test" << std::endl; mMutex.unlock(); } }; int main() { Bb b; b.printTest2(); return 0; } // $ g++ Test.cpp // Test.cpp: In member function ‘virtual void Bb::printTest() const’: // Test.cpp:17:25: error: passing ‘const std::mutex’ as ‘this’ argument discards qualifiers [-fpermissive] // 17 | mMutex.lock(); // | ^ // In file included from /usr/include/c++/9/mutex:43, // from Test.cpp:2: // /usr/include/c++/9/bits/std_mutex.h:98:5: note: in call to ‘void std::mutex::lock()’ // 98 | lock() // | ^~~~ // Test.cpp:19:27: error: passing ‘const std::mutex’ as ‘this’ argument discards qualifiers [-fpermissive] // 19 | mMutex.unlock(); // | ^ // In file included from /usr/include/c++/9/mutex:43, // from Test.cpp:2: // /usr/include/c++/9/bits/std_mutex.h:115:5: note: in call to ‘void std::mutex::unlock()’ // 115 | unlock() // | ^~~~~~