cost 187 ms
无法将派生的原始指针分配给基 unique_ptr - Cannot assign derived raw pointer to base unique_ptr

我有一些代码看起来像这样: 整个代码太大(而且是机密),无法在此处发布,但这段代码总结得很好。 本质上,我有一个基类 class 和一些派生类。 我还有一个管理器,它使用指向该基数 class 的(智能)指针。 以及一个返回适当 Derived object 的工厂方法(尽管签名返回一个Base* ...

为什么使用虚拟继承的静态向上转换对于 GCC 总是正确的? - Why static upcast with virtual inheritance is always correct for GCC?

学习后: 为什么涉及虚拟继承时不能使用static_cast进行向下转换? 我期待下面的代码给我的结果表明 static_cast 是错误的,而 dynamic_cast 是正确的。 但是,gcc 8/9 给了我以下 reuslt,显示静态转换和动态转换都是正确的: 那么这里涉及的魔法是什 ...

对象构造为一个基类,方法计算为另一个基类(菱形继承和代码冗余) - object constructed as one base class, method calculated as other base class (diamond inheritance and code redundancy)

我有一个通过虚拟继承解决的 C++ 中的菱形继承方案。 基类general有两个属性和一些使用它们的方法。 specialrestriction限制是具有两个属性相等的特殊限制的general 。 特殊规则用特殊规则稍微修改了specialrule somemethod() 。 最后 ...

OOP 代码中的“标记为覆盖但不覆盖”问题 - "Marked as override but does not override" Problem in OOP Code

我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。 在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 ShowVolume())中重新定义的虚函数。 但是,当我重新定义 function 并尝试运行 ma ...

虚拟 Inheritance:接口和构造函数 - Virtual Inheritance: Interfaces and constructors

我正在使用 C++11。我正在尝试声明 2 个接口:B 和 C,每个接口都声明了一些要由子类实现的函数。 这两个接口都依赖于在公共 A class 中声明的变量和函数。即使这种相对简单的结构也会导致钻石继承问题。( https://www.PCPC.me/what-is-diamond-proble ...

C++ 成员和 vtable 顺序在菱形(多个)虚拟 inheritance - C++ Member and vtable order in diamond (multiple) virtual inheritance

我想知道菱形虚拟 inheritance 上 C++ 中成员变量和 vtable 指针的顺序。 考虑下面的 inheritance: 我想知道 class Derived3 的memory布局。 我在网上查看了以下链接: c++ 数据 alignment /会员订单 & inherita ...

使用虚拟 inheritance 时强制调用基本构造函数,尽管它永远不会被调用? - Forced to call the base constructor when using virtual inheritance although it will never be called?

我有一个 class Base ,它有一个参数化的构造函数和两个类Middle1和Middle2 ,它们实际上继承自Base (为了解决菱形问题)。 另外, class Foo继承自Middle1和Middle2 。 Foo 现在显式调用 Base 构造函数并传递参数。 但是,代码无法编译,因为 ...

在 static_cast 和 dynamic_cast 之间自动选择以获得最佳性能 - Automatic selection between static_cast and dynamic_cast for best performance

我必须使用一个 object 工厂来创建几种类型的新对象,每个对象都派生自多态基 class。每个 object 的类型是事先已知的,但是工厂返回基 class 上的指针。所以在构建之后我需要将该指针向下转换为特定 object class 的类型。在大多数情况下, static_cast可以完美地 ...

如何创建仅调用祖父构造函数的构造函数? - How to create constructor that calls to grand parent constructor only?

我在一个层次结构中有 3 个类(称为 A、B 和 C),其中 B 扩展 A,C 扩展 B。Class A 有一个带有单个参数的构造函数。 C 的定义要求调用 A 的构造函数,所以我试图通过在 B 中创建构造函数来做到这一点。但是,编译器告诉我 C 的构造函数必须同时初始化 A 和 B。这对我来说 ...

C++ 入门第 5 版。 虚拟 inheritance 和 ctor-init - C++ primer 5th ed. Virtual inheritance and ctor-init

在 C++ 引物第 5 版上。 第18章多和虚拟inheritance,我有这个问题: 练习 18.30:在 Base 中定义一个默认构造函数、一个复制构造函数和一个具有 int 参数的构造函数。 在每个派生的 class 中定义相同的三个构造函数。 每个构造函数都应该使用它的参数来初始化它的 B ...

具有两个接口的多个 inheritance,一个从第一个接口派生,一个派生的 class 实现第一个接口 - Multiple inheritance with two interfaces, one which derives from first, and a derived class implementing the first interface

抱歉描述不佳。 这是问题: 由于“'Bar”而失败:由于以下成员,无法实例化抽象 class:'int PureVirtualBase::IntFn(void) is abstract" 我不确定如何纠正这个问题。 我会认为 Bar 很好,因为它继承了 Foo 的 IntFn 实现(并根据 Pure ...

在重用基本模板实现中替代虚拟 inheritance - Alternative to virtual inheritance in re-using base template implementation

在我正在处理的一个项目中,我们正在使用一种模式,其中我们有定义接口的纯抽象类,以及可以使用不同模板参数实例化的模板实现,这些模板参数用于许多事情,包括依赖注入。 就像是: 我想问题零是这是否是已知/常见/正式模式,如果是,它的名称是什么? 通常出现的需求是扩展Base接口,例如Inherited ...

有没有办法将 reinterpret_cast 重新解释为 virtual derived* 并从父级调用 override? - Is there a way to reinterpret_cast to a virtual derived* and calling overriden from parent?

print()在调用get()时尝试访问this是因为vfptr查找吗? 换句话说,这不可能吗? 编辑:我知道我可以使用new或通过取消引用指针来创建一个新的mask_t 。 然后定义mask_t::this 。 我不想创建实例的原因是出于性能问题[这在我承认的示例中不可见]。 如果您想回答,请 ...

使用virtual inheritance outside diamond inheritance问题允许越级 - Use of virtual inheritance outside diamond inheritance problem to allow leapfrogging

考虑以下程序: 让 D1 实际上继承自 B 允许我从 D2 的构造函数调用 B 的构造函数,跳过 class D1。 除了钻石继承问题之外,虚拟 inheritance 的这种使用是否会导致未定义的行为或其他危害? 除了解决钻石inheritance问题外,我从未见过使用虚拟inheritance。 ...


 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM