2

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

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
leonidus
  • 71
  • 1
  • 4
  • 1
    Why did you put 15 empty lines at the end of your post? Why didn't you use the preview functionality? – Sebastian Mach Aug 22 '11 at 06:44
  • Formatting problem. I corrected it. sorry about that. – leonidus Aug 22 '11 at 06:46
  • I think (if I'm interpreting correctly) that you have a decently interesting question here but it's hard to interpret. Maybe you could reorganize it a bit? – Owen Aug 22 '11 at 06:49
  • 2
    it's unclear what's being asked – Cheers and hth. - Alf Aug 22 '11 at 07:29
  • if he is talking about having something inherit from multiple classes then just add another : then the class name. ALSO (if my guess as to what you are meaning is correct) then only in the "parent" will they need to be made virtual. – Rhexis Aug 22 '11 at 08:23
  • @alf I have added questions just so its clear what I am asking. – leonidus Aug 23 '11 at 05:01
  • @Flyphe I am talking about a problem specific to a Proxy pattern being followed in CPP codebase where the problem is in getting implementation object of the derived proxy class when its passed in as a reference to base class. – leonidus Aug 23 '11 at 05:11

1 Answers1

0

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.

boycy
  • 1,473
  • 12
  • 25