09 2012 档案

摘要:class BoolBase{public: operator bool() const { return true; }};class A : public BoolBase{};class B : public BoolBase{};int main(int argc, char *argv[]){ if (A() == A()); // Ok. if (A() == B()); // Ok, but may NOT you want. return 0;} Just loo... 阅读全文
posted @ 2012-09-21 17:28 walfud 阅读(221) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;template <typename T>class X{ T m_val;public: friend auto operator<<(ostream &o, const X &x) -> ostream &;};template <typename T>auto operator<<(ostream &o, const X<T> &x) -> ostream &{ return o 阅读全文
posted @ 2012-09-20 19:53 walfud 阅读(211) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;template <int N>class Array{ enum class InsertOp : int { NONE, INSERTION, QUICK }; // Three insert method to choose. template <InsertOp N> class IntToType {}; // Differenciate type by this class. ... 阅读全文
posted @ 2012-09-20 14:47 walfud 阅读(194) 评论(0) 推荐(1)
摘要:#include <iostream>#include <vector>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ vector<int> v; cout <<"before: " <<v.size() <<endl; v.push_back(0); cout <<"after: " <<v.size() <<endl; cout <<"before: & 阅读全文
posted @ 2012-09-19 15:59 walfud 阅读(446) 评论(1) 推荐(0)
摘要:#include <iostream>using namespace std;class Base{public: void foo();};class Derive : public Base{public: void foo() const { cout <<"Derive." <<endl; }};void Base::foo() // Defination of 'Base::foo' must be { static_cast<Derive *>(this)->foo();}int _tmain 阅读全文
posted @ 2012-09-18 11:03 walfud 阅读(170) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;class A{public: A() { cout <<"A::A()" <<endl; } auto foo() -> void { cout <<"A::foo(). m_x = " <<m_x <<endl; } int m_x = 100;};class B{public: B() { m_a.foo(); } // Destructive! 'm_a' has not be 阅读全文
posted @ 2012-09-14 09:55 walfud 阅读(220) 评论(0) 推荐(0)
摘要:class B{public: virtual ~B(){}};class D : public B{public: D(){}protected: virtual ~D(){}};int main(){ D d; // Illegal. B *pD = new D; // Ok. delete pD; pD = NULL; return 0;} As you see, the key is to disable create local variable by prtected access in cl... 阅读全文
posted @ 2012-09-13 19:54 walfud 阅读(151) 评论(0) 推荐(0)
摘要:Many programmers find that we can write a template constructor, but do Not know what the usage of. Let's see the coding below:class Helper{public: template <typename U> Helper(const U &other){} // Is it any meaningful? It's depends.}Intent:#include <iostream>using namespace s 阅读全文
posted @ 2012-09-13 10:24 walfud 阅读(264) 评论(0) 推荐(0)