- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides. [ Example:
struct A {
virtual void f(int a = 7);
};
struct B : public A {
void f(int a);
};
void m() {
B* pb = new B;
A* pa = pb;
pa->f(); // OK, calls pa->B::f(7)
pb->f(); // error: wrong number of arguments for B::f()
}—end example ]
除了我在上面强调的陈述外,这段话和例子对我来说都很清楚。当我读到这篇文章时,我的印象是它与该段的第一部分相矛盾,即虚函数调用在虚函数声明中使用由静态类型决定的默认参数指示对象的指针或引用。
最佳答案
它们并不矛盾,因为它们指的是不同的事物。
A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object.
这意味着:
#include <iostream>
struct Base
{
virtual void foo(int x = 3) { std::cerr << x << '\n'; }
};
struct Derived : Base
{
virtual void foo(int x = 4) { std::cerr << x << '\n'; }
};
int main()
{
Derived d;
Base* ptr = &d;
ptr->foo(); // prints 3, not 4
}
An overriding function in a derived class does not acquire default arguments from the function it overrides.
另一方面,这意味着以下内容:
#include <iostream>
struct Base
{
virtual void foo(int x = 3) { std::cerr << x << '\n'; }
};
struct Derived : Base
{
virtual void foo(int x) { std::cerr << x << '\n'; }
};
int main()
{
Derived d;
d.foo(); // doesn't compile; missing value for x
}
I get the impression that it contradicts the first part of the paragraph, i.e. that a virtual function call uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object.
没有。这意味着如果您通过其 Base 接口(interface)访问该对象,您将获得 Base 的默认参数……并且如果您通过其 Derived 接口(interface)访问该对象,而 Derived 没有默认参数自己的,你不会得到基地的。同一枚硬币的两个截然不同的面。
而且,公平地说,引用的标准文本给出了完全相同的示例。
关于c++ - [dcl.fct.default]/10 中的明显矛盾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44122476/
据我了解,旧的 JMM 实现惰性单调的 DCL(双重检查锁定)技巧已被破坏,但我认为新的 JMM 和 volatile 字段已修复它...... 但是在 this不错的文章,显然足够新,可以引用新的和
据我了解,旧的 JMM 实现惰性单调的 DCL(双重检查锁定)技巧已被破坏,但我认为新的 JMM 和 volatile 字段已修复它...... 但是在 this不错的文章,显然足够新,可以引用新的和
DML(Data Manipulation Language): 它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言
我有一个正在运行的测试脚本会产生一些错误,如下所示,我预计会出现这些错误。无论如何我可以阻止它们显示在屏幕上吗?我用 $ write sys$output 如果出现预期错误则显示。 我尝试使用 $ D
当需要单例时,静态字段是一个优雅的解决方案吗? class HelperSingleton { static Helper singleton = new Helper(); public s
我正在阅读 article关于双重检查锁定。讨论的问题如下: public class MyFactory { private static MyFactory instance; p
在 Oracle 站点中 Types of SQL Statements 授予和撤销属于 DDL,但在许多其他站点中,我在 DCL 中看到授予和撤销……哪个是正确的? 最佳答案 最好治疗GRANT和
我真的对 OpenVMS 的 DCL 中的一些语法感到困惑。例如,这些是一些让我感到困惑的行: $ wo = "write sys$output" 它是否创建了一个符号 wo 用于写入 sys$
我正在尝试构建一个非常基本的 AutoLisp 界面。我在这方面完全是个初学者,所以在从头开始编写代码失败后,我转向正确地学习 DCL。我遵循了本教程: http://www.afralisp.net
我在父组件中有一个事件。我想从使用 DCL loadintolocation() 创建的子组件调用该事件。我在子组件中遵循此逻辑来引发事件 @Output() myevent1: EventEmitt
看到很多人讨论SQL还分为四种类型,在这里知识普及一下,并总结下他们的区别吧。 1. DDL – Data Definition Language 数据库定义语言:定义数据库的结构。 其主要命
我已经在 autolisp 中创建了我的整个应用程序,好吧,是时候编译和保护我的代码了......,在 autolisp 中,要使用 DCL,你只需要加载它们,使用: (setq sample (lo
我只是在阅读实践中的并发性。我开始知道有必要在字段的双重检查锁定机制中使用 volatile 关键字,否则线程可以读取非空对象的陈旧值。因为有可能在不使用 volatile 关键字的情况下对指令进行重
免责声明:我不在实际生产代码中使用 DCL - 我只有学术兴趣。 我读过以下著名文章:The "Double-Checked Locking is Broken" Declaration 问题声明(我
[dcl.spec]/3 in c++1z (重点是我的): If a type-name is encountered while parsing a decl-specifier-seq, it
[dcl.fct.default]/10 : A virtual function call (10.3) uses the default arguments in the declaration
我正在使用 *ngFor 从组件数组渲染动态创建的组件用 this answer 中描述的组件 我正在尝试向组件添加一个删除按钮,这样我就可以在使用 myCompArray.splice(index,
[dcl.init]/(6.2) If T is a (possibly cv-qualified) non-union class type, its paddingbits are initial
我无法理解 A fix that doesn't work 中的以下代码片段。(我确实阅读了同一页上的解释)。 如果我们有 2 个同步块(synchronized block),这个 DCL 版本是如
我读过以下经典名篇:The "Double-Checked Locking is Broken" Declaration 我有一个问题: In fact, assuming that the comp
我是一名优秀的程序员,十分优秀!