开发者

Virtual functions in Proxy pattern in C++

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

  1. Instantiate the original class in the c'tor
  2. Will be passed around internally for any reference/pointer param passing across different internal classes
  3. To get the actual impl of the proxy class (i.e. to get A from PrxA we have an impl store which will give us A from PrxA and B from PrxB.

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 g开发者_Python百科et 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.


If I define few functions as virtual in A are those supposed to be defined as virtual even in PrxA?

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.)

`C::C(PrxA& A): pa(A),a(getImpl(A)) If I pass A it Will work great. No problem here. 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)

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.

What approach should I be taking here if I need to pass PrxB to the classes like C which is taking PrxA as reference?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜