gpt4 book ai didi

c++ - 引入派生类中使用基本成员函数的重载解析

转载 作者:行者123 更新时间:2023-12-01 15:13:19 25 4
gpt4 key购买 nike

根据标准中的一些引用:
[over.match.funcs]/4

[...] For non-conversion functions introduced by a using-declaration into a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter.



并考虑以下代码:

#include <iostream>
struct Base {
void show(double) {
std::cout << "0" << std::endl;
}
};
struct Test :Base {
using Base::show; //#1
void show(double) { //#2
std::cout << "1" << std::endl;
}
};
int main() {
Test t;
t.show(1.2);
}

根据我引用的标准,这意味着将类型为 Test的隐式对象参数作为 #1
对于 #1,声明应为 show(Test&,double)
对于 #2,声明应为 show(Test&,double)
因此,为了解决过载, #1的每个隐式转换序列与 #2的每个隐式转换序列是无法区分的, t.show(1.2)的调用将是模棱两可的,但是为什么 #2被调用,为什么呢?如果我错过标准中的某些内容,请纠正我。

最佳答案

Test::show(double)Base::show(double)具有相同的签名;它只是从基类中隐藏了一个。

对于using declaration

(强调我的)

If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.



从标准 [namespace.udecl]/14

When a using-declarator brings declarations from a base class into a derived class, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list ([dcl.fct]), trailing requires-clause (if any), cv-qualification, and ref-qualifier (if any), in a base class (rather than conflicting). Such hidden or overridden declarations are excluded from the set of declarations introduced by the using-declarator. [ Example:

struct B {
virtual void f(int);
virtual void f(char);
void g(int);
void h(int);
};

struct D : B {
using B::f;
void f(int); // OK: D​::​f(int) overrides B​::​f(int);

using B::g;
void g(char); // OK

using B::h;
void h(int); // OK: D​::​h(int) hides B​::​h(int)
};

...

关于c++ - 引入派生类中使用基本成员函数的重载解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61077076/

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