I want to understand the "this" pointer. I thought that the "this" pointer refers to the value of the class object. However, in the code below, I could see different values ββfor the "this" pointer:
#include <stdio.h> class InterfaceA{ public: virtual void funa() = 0; }; class InterfaceB{ public: virtual void funb() = 0; }; void globala(InterfaceA* obj){ printf("globalA: pointer: %p\n\r",obj); } void globalb(InterfaceB* obj){ printf("globalB: pointer: %p\n\r",obj); } class concrete : public InterfaceA, public InterfaceB{ public: void funa(){ printf("funa: pointer: %p\n\r",this); globala(this); globalb(this); } void funb(){ printf("funb: pointer: %p\n\r",this); globala(this); globalb(this); } }; int main(int argc, char *argv[]) { concrete ac; ac.funa(); ac.funb(); return 0; }
The output of this program gives:
funa: pointer: 0x7ffff67261a0 globalA: pointer: 0x7ffff67261a0 globalB: pointer: 0x7ffff67261a8 funb: pointer: 0x7ffff67261a0 globalA: pointer: 0x7ffff67261a0 globalB: pointer: 0x7ffff67261a8
Any help for understanding this.
Thanks.
source share