keywords: [C++]基础备忘:显式调用构造

最近两年一直在使用UE4 Stylized C++,标准C++的一些基础都快忘了。。。

示例代码:

#include <iostream>

class CA
{
public:
    //两种初始化成员变量的方法
    CA() : ia_(11)
    {
        fa_ = 0.f;
    }

    CA(int val) : ia_(val)
    {
        fa_ = 0.f;
    }

    //要想让子类能够访问,修饰符不可为private
protected:
    int ia_;

private:
    float fa_;
};

class CB : public CA
{

public:
    //显示调用父类的有参构造函数
    CB() : CA(33), ib_(22)
    {
    }

    int ib()
    {
        return ib_;
    }

    int ia()
    {
        return ia_;
    }

private:

    int ib_;
};

int main(int argc, char* argv[])
{
    CB b;
    std::cout << b.ia() << " " << b.ib() << std::endl;

    return 0;
}
How to call a parent class function from derived class function?
class left {
public:
    void foo();
};

class right {
public:
    void foo();
};

class bottom : public left, public right {
public:
    void foo()
    {
        //base::foo();// ambiguous
        left::foo();
        right::foo();

        // and when foo() is not called for 'this':
        bottom b;
        b.left::foo();  // calls b.foo() from 'left'
        b.right::foo();  // call b.foo() from 'right'
    }
};

Reference:
https://stackoverflow.com/questions/357307/how-to-call-a-parent-class-function-from-derived-class-function


君子独立不惭于影,独寝不惭于魂。---《晏子春秋》