// my_class.h class my_class { // ... all public and protected stuff goes here ... private: class impl; unique_ptr<impl> pimpl; // opaque type here };
// my_class.cpp class my_class::impl { // defined privately here // ... all private data and functions: all of these // can now change without recompiling callers ... }; my_class::my_class(): pimpl( new impl ) { // ... set impl values ... }
// スマートポインタshared_ptrの使い方: #if (__cplusplus >= 201103L) // C++11 #include <memory> using std::shared_ptr; using std::static_pointer_cast; using std::make_shared; #else // Boost #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> using boost::shared_ptr; using boost::static_pointer_cast; using boost::make_shared; #endif
【余りの計算を高速化する】 十進法において、偶数は下一桁が必ず偶数になる。 奇数は下一桁が必ず奇数になる。これは10 mod 2≡0という合同式の性質から導かれる。 10 mod 3≡1であるから、ある自然数Xを3で割ったときの余りは、 Xの各ケタを足しあわせたものを3で割ったときの余りに等しい。 このように余りの計算は合同式の性質により高速化が可能になることがある。