gpt4 book ai didi

c++ - 私有(private)继承和函数重载错误

转载 作者:太空狗 更新时间:2023-10-29 21:03:30 26 4
gpt4 key购买 nike

我遇到了一个问题,我将其缩小为以下代码:

class A
{
};

class B : private A
{
};

void f(A*)
{
}

void f(void*)
{
}

int main()
{
B b;
f(&b);
}

在 gcc 4.7 中出现以下错误:

error: ‘A’ is an inaccessible base of ‘B’

我知道 A 是不可访问的,但我希望编译器调用 f(void*)。这种行为是正常的还是我做错了什么?或者可能是编译器错误?

最佳答案

重载在访问检查之前 得到解决。因此编译器选择 f(A*) 作为合适的重载,然后确定 &b 不能转换为 A* 并给出错误信息。

关于c++ - 私有(private)继承和函数重载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13295555/

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