Home > Back-end > C more abstract parent class problem, a pointer to the parent class how to call another function of
C more abstract parent class problem, a pointer to the parent class how to call another function of
Time:04-01
I met a problem in the actual development, before contact, don't know how to deal with, hope to glad to have a brother Problem situation is as follows: Have multiple abstract parent class is a class, the local storage is one of the parent class pointer, now I want to use to another in the parent class method, what should I do?
# include & lt; iostream> using namespace std;
Class A { Public: Virtual void funcA () { cout} Private: Int v=5; };
Class B { Public: Virtual void funcB () { cout} Private: Int v=15; }; Class C: public A, public B {
};
Int main () { A * A=new C; A - & gt; FuncA ();//output 5 (a) (B *) - & gt; FuncB ();//output 5 //to output 15 should excuse me I how to do? }
CodePudding user response:
Just want to ask: how to use A * pointer to access method to A class B
CodePudding user response:
The following personal understanding, wrong welcome correct: (a) (B *) - & gt; FuncB (); Forced conversion type does not change to address pointer, the pointer to address or the original function list, if compile-time class A funA offset for * A + 0 x4 save function pointer Similarly funB migration for class B + 0 x4; So you better turn (a) (B *) - & gt; FuncB (); Is the address of the actual call (Int) & amp; C + 0 x4 and a - & gt; FuncA () address is the same, so you actually call or a - & gt; FuncA (); Not funcB ();
CodePudding user response:
Add new C; If A structure for A - & gt; Class A (class hierarchy) Class B (structure) To invoke a + B is a class a placeholder address, pointing to the structure is B, this should be able to debug the specific see