C++ 中代理模式中的虚拟函数
如果我有一个代理模式 A 类,并且其代理是 PrxA 类。 Question1
如果我在 A
中将几个函数定义为 virtual
,那么即使在 PrxA
中,这些函数也应该被定义为 virtual
吗?
现在,如果
Class B : public A
{
///code
}
我相信代理类也应该继承。
Class PrxB : public PrxA {
/// code
}
现在假设这些代理类具有以下规则
- 在 c'tor 中实例化原始类
- 将在内部传递任何跨不同内部类传递的引用/指针参数
- 获取代理类的实际实现(即获取
A来自
我们有一个 impl 存储,它将为我们提供来自PrxA
的PrxA
的A
和来自PrxA
的B
PrxB
。
现在有一个类C
,它的c'tor中以PrxA为参考
。 (getImpl(PrxA))
初始化的 C 类的本地成员,
PrxA& pa;
A& a;
如果我通过 A,它会很好地工作。
正在 当我将 B
传递给此类 C
时,获取 B
的 impl(在 C 的 c'tor 中初始化的第二个?)的最佳方法是什么? (注意 B
源自 A
)
我可以考虑在 getImpl(A)
中进行类似的转换,但看起来不太好 ?
A* getAImpl(PrxA& pa)
{
if (implA(pa) != NULL)
return A;
else
return dynamic_cast<B>(A); // can't do this. since A will be returned but I actually need B
}
如果我需要通过,我应该采取什么方法 PrxB
到像 C
这样以 PrxA 作为参考的类?除了强制转换之外还有什么方法吗?
这里还有一件有趣的事情,如果我们限制为一个构造函数,我们可以得到 PrxA 或PrxB 的引用需要进行相应的处理才能在初始化器中获取 impl 我需要知道一个好的方法。
If I have a proxy pattern Class A
and proxy for that is Class PrxA
.
Question1
If I define few functions as virtual
in A
are those supposed to be defined as virtual
even in PrxA
?
Now if
Class B : public A
{
///code
}
I believe the proxy class should also inherit.
Class PrxB : public PrxA {
/// code
}
Now assuming these proxy classes have following rules
- Instantiate the original class in the c'tor
- Will be passed around internally for any reference/pointer param passing across different internal classes
- To get the actual impl of the proxy class (i.e. to get
A
fromPrxA
we have an impl store which will give usA
fromPrxA
andB
fromPrxB
.
Now there is a Class C
which takes PrxA as reference in its c'tor.
`C::C(PrxA& A): pa(A),a(getImpl(PrxA))
Local members of Class C which are being initialized.
PrxA& pa;
A& a;
If I pass A it Will work great. No problem here.
Question2
When I pass B
to this class C
what's the best way to get the B
's impl (the second initialized in C's c'tor? (note B
is derived from A
)
I can think of casting in getImpl(A)
something like this but doesn't look like a good soln.
A* getAImpl(PrxA& pa)
{
if (implA(pa) != NULL)
return A;
else
return dynamic_cast<B>(A); // can't do this. since A will be returned but I actually need B
}
What approach should I be taking here if I need to pass PrxB
to the classes like C
which is taking PrxA as reference? Is there any approach than casting.
Also interesting thing here if we restrict to one constructor, we can get PrxA or PrxB's reference which needs to be handled accordingly to get the impl in the initializers. I need to know a good approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您打算从 PrxA 派生,同时能够将 PrxA 视为多态基类时。 (从你帖子的其余部分来看,情况确实如此。)
这就是事情变得混乱的地方..你不能在这里传递 A除非 A 派生自 PrxA..您需要在措辞上更加精确,或者使用代码来解释
如果您传递 PrxB (再次,不是 B)并且 PrxB 派生自 PrxA (您已显示)。那么由于 PrxB 应该实现与 PrxA 相同的接口(应用里氏替换原理),因此您应该能够从 PrxB 获得完全相同的 A& 这包括您需要将 A& 获取到 B 实例的情况。源自 A。
正如您所表明的,PrxB 源自 PrxA,那么这应该已经可以正常工作了,因为您正在使用 PrxA&可以引用 PrxB 的实例,没有问题。
Only if you intend to derive from PrxA whilst being able to treat PrxA as a polymorphic base class. (It looks from the rest of your post that this is the case.)
This is where things get confusing..you can't pass A here unless A derives from PrxA..you need to be a little more precise in your wording, or use code to explain.
If you are passing PrxB (again, not B) and PrxB derives from PrxA (which you have shown) then since PrxB should be implementing the same interface as PrxA (applying Liskov Substitution Principle) you should be able to get an A& from PrxB exactly the same. This includes the case you require of getting an A& to B instance, assuming that B derives from A.
As you've shown that PrxB derives from PrxA then this should work fine already since you're taking a PrxA& which can be a reference to an instance of PrxB no problem.