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

CodePudding user response:

refer to the second floor gouyanfen 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 ();

Thank you for your advice, understand, so if you want to pass A pointer to access B function, only through A class A address offsets to call A class B function? To do so would be a little too much trouble,,,
My demand is this:
Because A is A core of the base class, has derived A lot of children, but now I want to add an extension, so add the class B (similar to the interface), I want to let those who have achieved A derived class, just inherited B B, realize pure virtual function to achieve the purpose of extending

But I realize and find that I converted to class A pointer to A class B calls, A memory leak, after debugging positioning to found that the problem is as you say, so I think excuse me you should know how to realize the call? Or on demand, can you give a implementation of other ideas, thank you very much

CodePudding user response:

Thanks brother above advice:
Use the following method can indeed call is successful, but some trouble, do not know to have brother there are better ways
 
((B *) (+ sizeof (char *) (a) (a))) - & gt; FuncB ();

CodePudding user response:

(C *) a - & gt; FuncB ();

CodePudding user response:

Can also be like this:
 int main () 
{
A * A=new C;
A - & gt; FuncA ();//output 5
(a) (B *) - & gt; FuncB ();//output 5
((C *) a) - & gt; FuncB ();//15

B * B=new C;
B - & gt; FuncB ();//15
(b) (C *) - & gt; FuncA ();//5
//to output 15 should excuse me I how to do?

}
  • Related