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...
阅读全文
摘要:#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
阅读全文
摘要:#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. ...
阅读全文
摘要:#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: &
阅读全文
摘要:#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
阅读全文
摘要:#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
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
浙公网安备 33010602011771号