Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C++ FAQ++ #TechLunch

C++ FAQ++ #TechLunch

2012/02/22(水) @ Livesense TechLunch
発表者:桂 大介

Livesense Inc.

April 23, 2014
Tweet

More Decks by Livesense Inc.

Other Decks in Technology

Transcript

  1. #include <iostream> class Foo { public: Foo(int n): _n(n) {}

    int get() { return _n; } private: int _n; }; int get_foo_address() { int foo_address = reinterpret_cast<int>(new Foo(11211)); return foo_address + 3306; } int main() { int foo_address = get_foo_address() - 3306; Foo* foo = reinterpret_cast<Foo*>(foo_address); std::cout << foo->get() << std::endl; // 11211 return 0; }
  2. void cout_n(int n) { cout << n << endl; }

    void cout_const_pn(const int* n) { cout << *n << endl; } void cout_mutable_pn(int* n) { cout << *n << endl; } int main() { const int* n = new int(123); cout_n(*n); cout_const_pn(n); cout_mutable_pn(const_cast<int*>(n)); return 0; }
  3. const Variable & Function variable • const int n =

    1; pointer • const int* n = new int(1); • int* const n = new int(1); • const int* const n = new int(1); function • const int* const plus3(const int n)
  4. class TextBlock { public: TextBlock(std::string t): text(t) {} const char&

    operator[] (std::size_t position) const { return text[position]; } char& operator[] (std::size_t position) { return text[position]; } private: std::string text; }; int main() { TextBlock tb("Hello"); const TextBlock ctb("Hello"); tb[1] = 'a'; ctb[1] = 'a'; // Error! std::cout << tb[1] << std::endl; std::cout << ctb[1] << std::endl; return 0; }
  5. class TextBlock { public: TextBlock(std::string t): text(t) {} const char&

    operator[] (std::size_t position) const { return text[position]; } char& operator[] (std::size_t position) { return const_cast<char&>( static_cast<const TextBlock&>(*this)[position]   ); } private: std::string text; }; int main() { TextBlock tb("Hello"); const TextBlock ctb("Hello"); tb[1] = 'a'; ctb[1] = 'a'; // Error! std::cout << tb[1] << std::endl; std::cout << ctb[1] << std::endl; return 0; }
  6. RTTI(実行時型情報) • Run-Time Type Identification • typeid(*p).name(); でクラス名が取れる • get_classとかinstanceof的なもの

    • 多重継承や例外と同様多くの議論を招いた 反対派 • 要らない。C++の精神に反している。複雑で難し い。 賛成派 • 一部の人にとっては重要。使わない人には無 害。実装しないと勝手実装される。
  7. template<class T> class smart_ptr { public: smart_ptr(): ptr(0) {} smart_ptr(T*

    ptr): ptr(ptr) {} ~smart_ptr() { delete ptr; } T& operator*() const { return *ptr; } T* operator->() const { return ptr; } private: T* ptr; }; template<class T> smart_ptr<T> smartptr(T *p) { smart_ptr<T> ptr = smart_ptr<T>(p); return ptr; }
  8. 参照カウント(Reference Counted)とは $a = "foo"; // refcount=1 • $b =

    $a; // refcount=2 • $c = $a; // refcount=3 • unset($c); // refcount=2 • unset($b); // refcount=1 • unset($a); // refcount=0 -> 値破棄
  9. template <class T> class Ptr { T *p; int *cnt;

    public: Ptr(T *p) : p(p) { cnt = new int(1); cout << "count=" << *cnt << endl; } Ptr(const Ptr<T> &self) { p = self.p; cnt = self.cnt; (*cnt)++; cout << "count=" << *cnt << endl; } ~Ptr() { (*cnt)--; cout << "count=" << *cnt << endl; if (*cnt < 1) { delete p; delete cnt; cout << "delete!" << endl; } } T *ptr() { return p; } };
  10. template <class T> Ptr<T> foo(T *p) { cout << "in

    foo()" << endl; Ptr<T> ptr = Ptr<T>(p); cout << "out foo()" << endl; return ptr; } int main() { { cout << "in loop1" << endl; Ptr<int> p1 = foo<int>(new int(100)); cout << *p1.ptr() << endl; { cout << "in loop2" << endl; Ptr<int> p2 = p1; cout << *p2.ptr() << endl; cout << "out loop2" << endl; } cout << "out loop1" << endl; } return 0; }