一些不常用的C++特性

来源:互联网 发布:大学生网络创业风险 编辑:程序博客网 时间:2024/06/03 18:08

案例1:

代码:
namespace N {    int i = 4;    extern int j;}int i = 2;int N::j = i;  // j 的值应该是多少?


案例2:

代码:

typedef int f;namespace N {    struct A {        friend int f(A&) { return 1; }        operator int()   { return 2; }        void g(A a) {            int i = f(a); // i 应该等于多少呢?        }    };}


案例3:

代码:
namespace M {    struct S {};    void f(S){};    // #1}namespace N {    void f(M::S){}; // #2    void g() {        M::S s;        M::f(s);   // 调用#1        N::f(s);   // 调用#2        // f(s);      // 会调用#2吗?#1和#2居然都匹配,编译报错!        (f)(s);     // 调用#2,    }}



案例4:

代码: 

namespace X {    class B;    namespace Y {        struct A {};        void foo(int (B::*)()) {}    }    struct B : Y::A {};}namespace N {    void g() {        int (X::B::*pd)() = 0;        foo(pd); // 不用名字空间修饰(X::Y::foo(pds))居然也可以    }} 



案例5:

代码:
void foo() try {} catch(...) {}struct X {    int data;    X() try {} catch(...) {}    X(int x) try : data(x) {} catch(...) {} // 你见过这样的函数吗?}; 



0 0
原创粉丝点击