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.