Problem of conversion of confusion point class
Code:
class a{public : virtual void f () {cout & lt;< "A" << Endl; }};class B: public a{public : virtual void f () {cout & lt;< "B" << Endl; }}; int _tmain (int argc, _tchar* argv[]) {* PA =
new A (); Pa->f (); b* PB = (b*) pa; Pb->f (); Delete PA,PB; PA =
new B (); Pa->f ();
//polymorphic PB = (b*) pa; Pb->f ();}
What happens to the following statement.
B* pb = (B*)pa;
Parse: In fact, what changes did not occur, or output A, there is no coverage problem, PB point to PA original address.
Does the return value type in the virtual function be different and can be overwritten?
A: Cannot overwrite, the derived class overrides the virtual function of the base class, and the return value type must also be the same.
The base class defaults to constructing whether the derived class needs to display the call
Does the following code error?
class A{public: A(intpublic A{public: B(){}};
Explanation, in cases where the base class does not have a default construct, the derived class needs to be explicitly called. So the above code compilation does not pass.
Conversion issues between base classes and derived classes
What is the result of the following program:
classa{ Public:intM_a;};classb{ Public:intM_b;};classC: PublicA Publicb{ Public:intM_c;};int_tmain (intARGC, _tchar* argv[]) {c* PC =NewC Pc->m_a =1; Pc->m_b =2; Pc->m_c =3; b* PB =dynamic_cast<B*> (PC); A * PA =dynamic_cast<A*> (PC); B PBB = *pc;//What will happen here? cout<< pc << Endl;cout<< PB << Endl;cout<< PA << Endl;cout<< &pbb <<"sizeof:"<<sizeof(PBB) <<"M_b"<< Pbb.m_b << Endl;if(PC = PB)cout<<"Equal"<< Endl;Else cout<<"Not Equal"<< Endl;if((int) PC = = (int) PA)cout<<"Equal"<< Endl;Else cout<<"Not Equal"<< Endl;}
Parse: Each statement is interpreted as follows,
1. A* pa = dynamic_cast<A*>(pc);
at this point the PA points to the section of subclass a, the address value is the same as the PC.
2. B pbb = *pc;
cutting occurs here, the B-type copy construction is called, and the part of B is cut into the stack space where the PBB is located.
3. if (pc == pb)
implicit type conversion occurs here, PC = (c*) PB
4. if ((int)pc == (int)pa)
Although there is no implicit type conversion, the address is the same.
dynamic_cast problems
There is a problem with which statement in the following code.
classa{ Public:Virtual voidFoo () {cout<<"A foo ()"<< Endl; }voidPP () {cout<<"A pp ()"<< Endl; }};classB: Publica{ Public:voidFoo () {cout<<"B foo ()"<< Endl; }voidPP () {cout<<"B pp ()"<< Endl; }voidFunb () {cout<<"B::funb ()"<< Endl;}};int_tmain (intARGC, _tchar* argv[]) {a A; A *pa = &a; (dynamic_cast<B*> (PA))->foo ();//Statement 1(dynamic_cast<B*> (PA))->pp ();//Statement 2(dynamic_cast<B*> (PA))->funb ();//Statement 3System"Pause");return 0;}
Parsing: A problem occurs with statement 1. Foo () is a virtual function, and the compiler locates the Foo function by looking up the virtual function table based on the object's virtual function pointer.
Dynamic_cast is not forced type conversion, but with some kind of "advisory" nature, if not converted, dynamic_cast will return null, indicating unsuccessful.
The above 3 statements are equivalent to:
B*=NULL; bnull->foo(); bnull->pp(); bnull->funB();
The above conversion is unsuccessful, so the null pointer is returned, and because the PP and FUNB functions do not use any member data, are not virtual functions, do not need the this pointer, and do not need to bind dynamically , so it can run normally.
Virtual inheritance and the distribution of inherited memory with virtual functions
What is the result of the following code output?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Difficult notes--difficult questions and confusion points