Home > Back-end >  A friend derived class reloading function
A friend derived class reloading function

Time:09-21

Class A
{
Friend class B;
Protected:
Int m_iA=0;
}

Class B
{
Protected:
A, m_a.
Virtual void func () {m_a. M_iA=1; }
}

Class C: class B
{
Virtual void func () {m_a. M_iA=2; }
}

In the above relations, inheritance from B, C C overloaded function func,
C func function overloading in the hope of its base class B member variables m_a do some different operations,
But for A friendly against A B yuan relationship cannot be C inheritance, so the actual operation is not feasible,

Question: what can you do to solve this problem? Hope to have more professional,

CodePudding user response:

Need to revise the B:
 
#include

Class A
{
Friend class B;
Protected:
Int m_iA;
};

Class B
{
Protected:
A, m_a.
Virtual void func (int I) {m_a. M_iA=I; }
Public:
Void the print () {printf (" % d \ n ", m_a m_iA); };
};

Class C: public B
{
Public:
Void test () {func (2); Print (); }
};

Int main ()
{
C C;
C.t est ();

return 0;
}

CodePudding user response:

reference 1/f, play big shoot early nuclear response:
need to revise the B:
 
#include

Class A
{
Friend class B;
Protected:
Int m_iA;
};

Class B
{
Protected:
A, m_a.
Virtual void func (int I) {m_a. M_iA=I; }
Public:
Void the print () {printf (" % d \ n ", m_a m_iA); };
};

Class C: public B
{
Public:
Void test () {func (2); Print (); }
};

Int main ()
{
C C;
C.t est ();

return 0;
}
if A and B are in the MFC class, I can't literally change the definition of these classes?
If you can, my friends and the C directly to the addition of A metaclass wouldn't it be more convenient?

CodePudding user response:

Such A request, if it is A Delphi, c # can give A, B patch, don't need to modify the source code (and even no source), but the standard c + +, can only use hacking method:
 
#include

Class A
{
Friend class B;
Protected:
Int m_iA;
};

Class B
{
Protected:
A, m_a.
Virtual void func () {m_a. M_iA=1; }
Public:
Void the print () {printf (" % d \ n ", m_a m_iA); }//just for test
};

Class C: public B
{
Private:
Struct IB {
Void * VPTR;
Int m_iA;
};
Public:
Virtual void func () {(this) (IB *) - & gt; M_iA=2; }
};

Int main ()
{
C C;
c.func();
C.p rint ();

return 0;
}

CodePudding user response:

I just tried to use this method to solve,
Don't know if it is not professional,

 class A 
{
Friend class B;
Protected:
Int m_iA=0;
}

Class B
{
Protected:
A, m_a.
Virtual void func () {m_a. M_iA=1; }
}

Class D: class A
{
Friend class C;
}

Class C: class B
{
Virtual void func () {((D *) & amp; M_a) - & gt; M_iA=2; }
}

CodePudding user response:

Are allowed, this is you grammar have a problem, what the compiler can pass?

CodePudding user response:

reference 5 floor early play the play nuclear war response:
is ok, is this grammar you have any question, what the compiler can pass?
what grammar problem? I am using VS2015

CodePudding user response:

Class D: class A
This grammar can pass?

CodePudding user response:

General public inheritance

CodePudding user response:

Class D: public A and class D: A is the same, but the class D: it's certainly not A class A,
  • Related