Hi, Thanks for indepth explanation. I analyzed the same but using windows assembly language. B1Cls obj; 0041141E lea ecx,[obj] 00411421 call B1Cls::B1Cls (411028h) obj.show(); 00411426 lea ecx,[obj] 00411429 call B1Cls::show (411140h) // shows compile time ,method call. B1Cls *ptr = new B1Cls(); 0041142E push 4 00411430 call operator new (41119Ah) 00411435 add esp,4 00411438 mov dword ptr [ebp-0E0h],eax 0041143E cmp dword ptr [ebp-0E0h],0 00411445 je main+5Ah (41145Ah) 00411447 mov ecx,dword ptr [ebp-0E0h] 0041144D call B1Cls::B1Cls (411028h) 00411452 mov dword ptr [ebp-0E8h],eax 00411458 jmp main+64h (411464h) 0041145A mov dword ptr [ebp-0E8h],0 00411464 mov eax,dword ptr [ebp-0E8h] 0041146A mov dword ptr [ptr],eax ptr->show(); // shows Runtime .. 0041146D mov eax,dword ptr [ptr] 00411470 mov edx,dword ptr [eax] 00411472 mov esi,esp 00411474 mov ecx,dword ptr [ptr] 00411477 mov eax,dword ptr [edx] 00411479 call eax 0041147B cmp esi,esp 0041147D call @ILT+340(__RTC_CheckEsp) (411159h) }
Please correct me if I am wrong .. GopiKrishna Komanduri Software engineer Hyderabad [email protected] --- On Fri, 4/9/09, Luciano Cattani <[email protected]> wrote: From: Luciano Cattani <[email protected]> Subject: Re: [c-prog] some basic queries about polymorphism To: [email protected] Date: Friday, 4 September, 2009, 3:50 AM --- Mer 2/9/09, Gopi Krishna Komanduri <gopikomanduri@ yahoo.com> ha scritto: Da: Gopi Krishna Komanduri <gopikomanduri@ yahoo.com> Oggetto: Re: [c-prog] some basic queries about polymorphism A: c-p...@yahoogroups. com Data: Mercoledì 2 settembre 2009, 20:09 class Base { public: virtual void show() { } } int main() { Base obj; obj.show() // I think complite time polymorphism comes here. Base *obj = new Base() obj->show() // RUN Time } > Please correct me if I am wrong . I would like to know why this happens > > like so. Polymorphism only happens when access to a memberfunction is done via pointers or references and _never_ when direct access is used. If you use only the Base class in your example then there is no difference between the two methods because you always call Base::show() but there is a _big_ difference in _how_ the function gets called. If you know a little assembly then write the example above and use the 'assembly-only' flag of GCC: $ gcc -S -o test.S test.cpp and look at the assembler code in the 'test.S' file. You may notice that every instance of a class which has virtual functions has an hidden data member that is a pointer to the virtual table of the constructed class. In our example the virtual table only contains one pointer: _vptr.Base ---------- the pointer to Base::show() In the first call - obj.show() - the assembly code is like this: call 0x8048532 A direct call to the address of Base::show() function is done because the variable 'obj' was declared as Base type This happens even if the actual object is of a derived type. Using pointer's access is totally different - obj->show(). This is the assembly code on my machine : mov eax,-10[%ebp] mov ebx,eax[2] mov edx,ebx call %edx Firt, the code loads some CPU registers with the pointer to the virtual table, this pointer is stored in the _real_ object so if it is a derived class then the virtual table of the derived class is loaded. The EDX register is loaded with the pointer to the show() function of the virtual table. Finally an _indirect_ call is done to the address pointed to by EDX. In other words, when access to memberfunctions is done directly then the type of the declared variable states the address of the function called. When access is done using pointers then the function called depends on the _actual_ type of the object not on the type of the declared variable used for accessing it. Luciano Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com [Non-text portions of this message have been removed]
