菱形多重继承仅具有功能
我想讨论c ++中的继承问题. 基本思想是拥有一个具有属性的基类,以及具有该属性访问器的不同子类.在底部应是一个类,它将子类组合为一个类.它是菱形,会带来一些问题.
i'd like to discuss an inheritance problem in c++. The basic idea is to have a base class with properties and different child classes with accessors to this properties. And at the bottom shall be a class, which combines the child classes to one. It's a diamond shape, which introduces some problems.
class A {
public:
A() { prop = 1; }
int prop;
};
class B : public A {
public:
int getProp() { return prop; }
private:
B();
};
class C : public A {
public:
int setProp(int v) { prop = v; }
private:
C();
};
class D : public B, public C {
private:
D();
};
usage() {
A* a = new A();
B* b = (B*)a; // Works
b->getProp(); // Works
D* d = (D*)a; // This fails, because of diamond inheritance
d->setProp(5);// This is kindof the goal
d->getProp();// This is kindof the goal
}
因此,我已经阅读了有关虚拟继承的内容,但是我无法实现对D的强制转换,它应该包括B和A的所有方法.也许我在那里缺少一些东西.
So, I've read something about virtual inheritance, but I cannot achieve the casting to D, which should include all methods of B and A. Maybe I'm missing something there.
另一方面.实际上,我在类B和C中仅具有函数,因此通常可以通过采用该类实例的静态包装器来实现这些访问器.但是,我想请您提出一些想法,在没有任何包装的情况下真正实现它,这些包装以原始A作为参数.
On the other hand. I really have only functions in the classes B and C, so in general it would be possible to achieve these accessors via a static wrapper taking a instance of the class. But I'd like to ask you for ideas to actually achieve it without any wrappers, which take the original A as an argument.
我将为您提供一些方案,以阐明我要实现的目标. 想象一下项目A,B,C,D和E
Edit 1: I will provide you with some scenario to clarify what I am trying to achieve. Imagine Project A, B, C, D and E
Project_A contains Class_A. This class contains some member variables.
P_A also creates 5 instances of the class.
Project_C contains Class_C.
P_C also asks P_A to get the 5 instances of C_A.
P_C sets the properties of the 5 instances via its accessors.
Project_B contains Class B.
P_B asks P_A to get the 5 instances of C_A.
P_B gets the properties of the 5 instances.
Project_D contains Class D.
P_D does set and get the properties via the accessors of C_B and C_C.
Project_E contains Class E.
Project_E works on a different property of A.
P_B knows P_A
P_C knows P_A
P_D knows P_A, P_B, P_C
P_E knows P_A
这些项目是由不同的方面进行的.
These Projects are worked on by different parties.
也许这会有所帮助.也许不是.让我们来看看.
Maybe this helps. Maybe it doesn't. Lets see.
这里是另一个伪代码示例.也许这样对您确实更有意义.关键可能是A不知道它拥有哪些属性. (或更确切地说,它们的意思)
Edit 2: Here another pseudo code example. Maybe this way it does make more sense to you. The key may be, that A does not know which properties it holds. (or more precisely, what they mean)
class A {
map<int, int> props;
}
class B : private A {
getProp1() { return props(1)}
setProp1(int prop) { props(1) = prop }
}
class C : private A {
getProp2() { return props(2)}
setProp2(int prop) { props(2) = prop }
}
class D : public B, public C {
// This is just a combination of B and C for comfort.
}
class E : private A {
getProp3() { return props(3)}
setProp3(int prop) { props(3) = prop }
}
因此,显然没人在乎了.所以这是我想规避的解决方案.
So, obviously nobody seems to care anymore. So here is the solution I wanted to circumvent.
class A {
public:
map<string, string> keyvalue;
}
class B {
static string getProp1(A* a) { return a->keyvalue.find("prop_1")->second;}
}
class C {
static string getProp2(A* a) { return a->keyvalue.find("prop_2")->second;}
}
class BandC : public B, public C {
//THis is just a combination of B and C
}
由于类B和C仅包含函数,因此从逻辑上讲,不需要它们的真实实例.它们基本上可以在A类的实例上进行操作.
Since classes B and C only contain functions, there would have been logically no need for a real instance of them. They can basically operate on an instance of class A.
如果不清楚.如果我从A继承B和C并再次在BandC中将它们组合,则由于菱形会出现错误.
In case it is not clear. If I inherit B and C from A and combine them again in BandC, I get the error because of the diamond shape.