我有这样的公共接口层次结构:
struct ISwitchable {
/* Obtain pointer to another implemented interface of the same instance. */
virtual int switch(unsigned int interfaceId, void** pInstance) = 0;
};
struct IFoo : public ISwitchable { /* Methods */ };
struct IBar : public ISwitchable { /* Methods */ };
struct IFooBar : public IFoo, public IBar { /* Methods */ };
Run Code Online (Sandbox Code Playgroud)
实现IFooBar的类与工厂函数一起放入dll.客户端代码加载dll,使用工厂函数创建类实例并根据接口使用它们(它们作为头文件提供).
Scheme使用MSVC制作的dll和Borland C++ Builder 6制作的客户端代码可以正常工作.
我将虚拟继承引入层次结构:
struct IFoo : public virtual ISwitchable { /* Methods */ };
struct IBar : public virtual …Run Code Online (Sandbox Code Playgroud) 所以我有这个想法,我认为用C++实现基本上是不可能的...但我想问一下.我读完Stroustrup的第15章并没有得到我的答案,我认为关于继承钻石的其他十亿个问题没有回答这个问题,所以我在这里问.
问题是,当你从两个共享一个公共基类的基类继承时会发生什么,但是这两个基类中只有一个虚拟地从它继承.例如:
class CommonBase { ... };
class BaseA : CommonBase { ... };
class BaseB : virtual CommonBase { ... };
class Derived : BaseA, BaseB { ... };
Run Code Online (Sandbox Code Playgroud)
我认为我想这样做的原因是因为我试图扩展现有的库而不必重新编译整个库(不想打开那些蠕虫).已经存在我想要修改的继承链.基本上是这样的(原谅ascii艺术)
LibBase
| \
| \
| MyBase
| |
| |
LibDerived |
| \ |
| \ |
| MyDerived
| |
LibDerived2 |
| \ |
| \ |
| MyDerived2
| |
LibDerived3 |
| \ |
| \ |
| MyDerived3
| |
LibConcrete | …Run Code Online (Sandbox Code Playgroud) 我正在阅读关于继承的内容,我有一个我几个小时都无法解决的重大问题:
鉴于一个类Bar是一个具有virtual函数的类,
class Bar
{
virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)
有什么不同:
class Foo : public Bar
{
virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)
和
class Foo : public virtual Bar
{
virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)
?谷歌搜索和阅读的时间提供了大量有关其用途的信息,但实际上没有人告诉我两者之间有什么区别,只是让我更加困惑.
我经常使用纯虚拟类(接口)来减少当前项目中不同类的实现之间的依赖关系.我甚至拥有层次结构并不罕见,在这些层次结构中我有纯虚拟和非纯虚拟类来扩展其他纯虚拟类.以下是这种情况的一个例子:
class Engine
{ /* Declares pure virtual methods only */ }
class RunnableEngine : public virtual Engine
{ /* Defines some of the methods declared in Engine */ }
class RenderingEngine : public virtual Engine
{ /* Declares additional pure virtual methods only */ }
class SimpleOpenGLRenderingEngine : public RunnableEngine,
public virtual RenderingEngine
{ /* Defines the methods declared in Engine and RenderingEngine (that are not
already taken care of by RunnableEngine) */ }
Run Code Online (Sandbox Code Playgroud)
双方RunnableEngine并RenderingEngine延长 …
请告诉我为什么以下程序的输出如下.我没有在c ++中获得虚拟类.请遵守以下代码:
class B
{
public:
B(char c = 'a') : m_c(c) {}
public:
char get_c() const { return m_c; }
void set_c(char c) { m_c = c; }
private:
char m_c;
};
class C: public B
{ };
class D: public B
{ };
class E
: public C
, public D
{ };
int main()
{
E e;
C &c = e;
D &d = e;
std::cout << c.get_c();
d.set_c('b');
std::cout << c.get_c() << std::endl;
return 0;
} …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#...
class A {};
class B: public A{};
class C: virtual public A{};
class D: virtual public C{};
// No More Classes
...
int _tmain(int argc, _TCHAR* argv[]) {
cout<<sizeof(A)<<" ";
cout<<sizeof(B)<<" ";
cout<<sizeof(C)<<" ";
cout<<sizeof(D)<<".";
...
}
Run Code Online (Sandbox Code Playgroud)
O/P:1 1 4 8.
题:
*.这是我的第一个问题,如果你发现任何错误,请纠正我.
请参见以下代码:
struct Object;
struct Component
{
Component(Object* obj) { }
};
struct Renderable : public virtual Component
{
virtual void Render() = 0;
};
struct AnimationRenderer : public Renderable
{
AnimationRenderer(Object* obj) : Component(obj) { }
virtual void Render() { }
};
Run Code Online (Sandbox Code Playgroud)
由于没有与Component::Component()from的匹配调用,因此无法编译Renderable::Renderable()。
我可以通过提供Renderable一个构造函数来使此示例工作Renderable() : Component(NULL) { },即使Renderable将永远无法初始化Component。
因为Renderable是抽象类,所以永远不能直接实例化它。由于它实际上是从继承的Component,因此它将永远无法调用的初始化Component。
语言要求永远/永远不能调用代码的原因是什么?
我试图通过虚拟继承和vTables/vPtrs完全理解内存中发生的事情,什么不是.
我有两个我编写的代码示例,我完全理解它们的工作原理,但我只想确保在脑海中对对象内存布局有正确的想法.
以下是图片中的两个示例,我只想知道我对所涉及的内存布局的想法是否正确.
例1:
class Top { public: int a; };
class Left : public virtual Top { public: int b; };
class Right : public virtual Top { public: int c; };
class Bottom : public Left, public Right { public: int d; };
Run Code Online (Sandbox Code Playgroud)

例2:
与上述相同,但有:
class Right : public virtual Top {
public:
int c;
int a; // <======= added this
};
Run Code Online (Sandbox Code Playgroud)

c++ oop inheritance multiple-inheritance virtual-inheritance
这似乎是一个基本问题,但我没有看到它问:
假设以下简单案例:
没有虚拟成员.
虚拟继承用于允许多个路径到同一个基础.
根据访问派生类最多的成员所需的时间,虚拟继承的价格是多少?特别是,如果存在非零价格,它是否仅适用于通过多个路径继承的成员或其他成员?
当使用clang或gcc(在macOS上)编译时,以下代码似乎运行正常,但在使用MS Visual C++ 2017编译时崩溃.在后者上,foo_clone对象似乎已损坏,程序崩溃时出现访问冲突foo_clone->get_identifier().
如果我删除协变返回类型(所有clone-methods返回IDO*),或何时std::enable_shared_from_this删除,或者所有继承都是虚拟的,它确实适用于VC++ .
为什么它适用于clang/gcc而不适用于VC++?
#include <memory>
#include <iostream>
class IDO {
public:
virtual ~IDO() = default;
virtual const char* get_identifier() const = 0;
virtual IDO* clone() const = 0;
};
class DO
: public virtual IDO
, public std::enable_shared_from_this<DO>
{
public:
const char* get_identifier() const override { return "ok"; }
};
class D : public virtual IDO, public DO {
D* clone() const override {
return nullptr;
}
};
class IA …Run Code Online (Sandbox Code Playgroud) c++ ×10
inheritance ×4
visual-c++ ×2
c++11 ×1
c++14 ×1
c++builder ×1
clang++ ×1
oop ×1
polymorphism ×1
pure-virtual ×1
virtual ×1