lundi 30 juillet 2018

Casting of object pointers

obj is pointer of type B pointing to an object of D. But, what is the difference between the pointer returned by obj->getA() and obj pointer . "this" pointer returns the address of calling object which should be obj, but the return type is A*. So what exactly does obj->getA() represent , and where such a scenario can be useful ?

class A{
};

class B{
  virtual A* getA() { return NULL; }
};

class C : public A, public B{
   A* getA() { return this; }
}

class D : public C{
}

int main(){
 B *obj = new D();
 obj->getA();
}

Aucun commentaire:

Enregistrer un commentaire