oplaTech Oplatek Teaching Archive
Oplatek's external memory

C++ hints


  • How to convert number to string effectively and back?

    lexical_cast<int>(my_string);
    lexical_cast(my_int);

  • In templates always write

    template< typename T > class X : public T {
    void f() { return this->m();} // best use, calls parent or current method(virtual)
    void h() {return m(); } // calls possibly global method m()!!!
    void g() { return T::m();// calls parent method but disables virtual method
    }

  • So called Policy class in associative containers

    map<K,T,cmp> // cmp funktor if there is no operator "<" for K
    // so we need cmp
    struct cmp {
    bool operator () (K a, K b) { return /* correct ordering */ }
    }

    Problems: Policy class creates object cmp, and operator is not static as Policy class should have