GeekBand-C++面向对象高级编程(下)-Week1

转换函数

class A {
public:
   operator double() {
        // return a double value
    }
};

explicit关键字

class A {
public:
    explicit A(int a) {}
};

指定构造函数需要显式调用,这样编译器就不会做隐式转换,避免因one-argument构造函数而导致意料之外的类型转换。

pointer-like class

class A {
public:
    T& operator*() {
        return *data;
    }
    T* operator->() {
        return data;
    }
private:
    T* data;
};

function-like class

class A {
public:
    operator() {}
};

你可能感兴趣的:(GeekBand-C++面向对象高级编程(下)-Week1)