0

How to have common pointer for independent classes

Hi I have a class ( for eg. clsMain) which is responsible currently to call the methods of seperate class (For eg. clsA) . This class clsMain has pointer of the clsA class and it is working fine Now scenario is that I need to work with clsB also and clsMain class based on input decides what to call for either clsA or clsB I don't have control over clsA and clsB as it is owned by other group How can I better use it in to clsMain ? I don't wana store pointer of both class but I don't find a way to store it in a single pointer due to the fact that they are independent classes Any suggestions on this would be great.

25th Dec 2021, 9:32 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
10 Antworten
+ 2
Ketan Lalcheta it seems (at least to me) that this is a problem that can be solved using dynamic inheritance. Have clsMain define an interface( a set of virtual functions ) and then let clsA and clsB define the implementation ( override the virtual functions ). But since you've said that you don't have control over clsA and clsB, then the only solution is to define clsMain as having 2 pointers/objects of the 2 classes as member variables.
26th Dec 2021, 3:19 PM
Anthony Maina
Anthony Maina - avatar
+ 1
Anthony Maina Thanks. this is something I will try.... But having said that, what's the difference if I access clsA method directly or access clsA object from clsMain I still have to figure out which object or pointer of clsMain to use... Correct or I m missing something ?
26th Dec 2021, 6:42 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Correct. You still have to figure out which object to use, whether you are accessing their methods directly or through a clsMain object.
27th Dec 2021, 5:36 AM
Anthony Maina
Anthony Maina - avatar
0
You may define your own type : struct clsAB { clsA a; clsB b; }; clsAB ab; ab.a; //access clsA ab.b; //access clsB
25th Dec 2021, 4:36 PM
VCoder
VCoder - avatar
0
Yes that's correct ... But what benifit we will get by doing so ? Still I have to use condition to decide whether to write ab.a.function() or ab.b.function() I need some mechanism which will work like ab.function which itself decided on a or b
25th Dec 2021, 5:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
A struct is like a class without methods, you know ? What's the condition to use one of these classes
25th Dec 2021, 5:05 PM
VCoder
VCoder - avatar
0
That is user input... If input is 1 , then call a or else call b.. I will be calling this class a or class b method almost 100 times in entire code So don't want to call each time using the if else condition
25th Dec 2021, 5:08 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Does their methods have the same names ?
25th Dec 2021, 5:09 PM
VCoder
VCoder - avatar
0
Yes it has... To be more specific, only one method is not to be called Method1, method2 and method3 are called so many times
25th Dec 2021, 5:11 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
- 1
Is it what you want ? : if(input) A ab(); else B ab(); ab.method1(); //this will work
25th Dec 2021, 5:22 PM
VCoder
VCoder - avatar