【问题标题】:why we use Using Declaration in derived class? [duplicate]为什么我们在派生类中使用 Using Declaration? [复制]
【发布时间】:2011-09-21 00:51:48
【问题描述】:

可能重复:
Why shall I use the “using” keyword to access my base class method?

using 声明将基类中的数据成员或成员函数的名称引入派生类的范围内,这在我们从基类派生类时隐式完成,那么使用“使用声明”有什么用处?

我想深入了解 c++ 类中 using 声明的使用。

【问题讨论】:

标签: c++


【解决方案1】:
struct Base()
{
   void f(char);
};

struct Derived: Base
{
   void f(int);
};

int main()
{
   Derived d;
   d.f('a');
}

你认为哪个会被调用?似乎调用了f(int),因为名称 f 将名称 f 隐藏在 Base 中。所以你需要一个 using 声明来启用它。

struct Derived: Base
{
   using Base::f;
   void f(int);
};

现在将调用f(char)

这是一个例子。高温

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2010-09-17
    • 2020-01-29
    相关资源
    最近更新 更多