gpt4 book ai didi

c++ - using-declaration 无法正常工作

转载 作者:IT老高 更新时间:2023-10-28 23:13:34 24 4
gpt4 key购买 nike

在以下示例中,我试图隐藏 using Employee::showEveryDept来自最后一个 child 类Designer通过在类里面设为私有(private) Elayer -

#include <iostream>

class Employee {
private:
char name[5] = "abcd";
void allDept() { std::cout << "Woo"; }

public:
void tellName() { std::cout << name << "\n"; }
virtual void showEveryDept()
{
std::cout << "Employee can see every dept\n";
allDept();
}
};

class ELayer : public Employee {
private:
using Employee::showEveryDept;

protected:
ELayer() {}

public:
using Employee::tellName;
};

class Designer : public ELayer {
private:
char color = 'r';

public:
void showOwnDept() { std::cout << "\nDesigner can see own dept\n"; }
};

int main()
{
Employee* E = new Designer;
E->showEveryDept(); // should not work

Designer* D = dynamic_cast<Designer*>(E);
D->showOwnDept();
}

但它仍在编译,输出是 -

Employee can see every dept
Woo
Designer can see own dept

但我已明确将其设为私有(private),请参阅 - private: using Employee::showEveryDept;

我在这里做错了什么?

最佳答案

你想错了。

C++ 有 Name Lookup 的概念,这是一个构建良好的概念,我们通常不会想到,但这在任何地方都会发生 name用来。通过这样做:

int main()
{
Employee* E = new Designer;
E->showEveryDept(); // should not work

Designer* D = dynamic_cast<Designer*>(E);
D->showOwnDept();
}

E->showEveryDept() 行对属于 E 类的成员(在本例中为成员函数)进行非限定名称查找。因为它是 accessible name , 该程序是合法的。


我们也知道 Designer 是从 ELayer 派生的,其中 showEveryDept() 被声明为 private在这里做了:

class ELayer : public Employee {
private:
using Employee::showEveryDept;

protected:
ELayer() {}

public:
using Employee::tellName;
};

但您所做的只是明确地 introduceEmployee 类中的名称 showEveryDept() 转换为 Elayer;引入 private 访问。这意味着,我们不能在类成员/静态函数之外直接访问与 ELayer 关联的名称(或调用该函数)。

ELayer* e = new Designer();
e->showEveryDept(); //Error, the name showEveryDept is private within ELayer

但是,由于 showEveryDept()Elayer 的基类中具有 public 访问权限,因此 Employer;仍然可以使用 qualified name lookup 访问它

ELayer* e = new Designer();
e->Employer::showEveryDept(); //Ok, qualified name lookup, showEveryDept is public

Elayer 中可在 Designer 中访问的名称将由其 access specification 决定.如您所见,名称 showEveryDept() 是私有(private)的,因此 Designer 甚至不能使用这样的名称。

对于您当前的类层次结构和定义,这意味着:

Employee* E = new Designer();
ELayer* L = new Designer();
Designer* D = new Designer();

-- 对于 unqualified lookup :

  • 这行得通

    E->showEveryDept();                // works!
  • 这失败了:

    L->showEveryDept();                // fails; its private in Elayer
  • 这也失败了:

    D->showEveryDept();                // fails; its inaccessible in Designer

-- 对于 qualified lookup ,在这种情况下,请求从其基类调用此类函数:

  • 这失败了:

    D->Elayer::showEveryDept();        // fails! its private in Elayer
  • 这行得通:

    D->Employee::showEveryDept();      // works! its accessible in Employee
  • 这也有效:

    L->Employee::showEveryDept();      // works! its accessible in Employee

注意:将任何成员函数的名称,B::func(例如)从基类 B 引入派生类 D ,不会覆盖 B::func,它只是让 B::func 在派生类的上下文中对重载决议可见。查看 this question 的答案和 this

关于c++ - using-declaration 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com