Link to home
Start Free TrialLog in
Avatar of Ponga_Pundit
Ponga_Pundit

asked on

Polymorphism with double pointer

The code snippet below doesn't compile( VC++ 6 compiler). The error is: error C2664: 'myfn' : cannot convert parameter 1 from 'class D ** ' to 'class A ** '
--------------
class A {};
class D : public A {};

void myfn(A** ap) {}

void main()
{
      D d;
      D *pd=&d;
      myfn(&pd);
}
--------------
But if I change the myfn() parameter to A**, and change the call to myfn(pd), it works. Why the polymorphism doesn't seem to work with double pointer?
Avatar of balder
balder
Flag of Norway image

I guess you will find the explanation in the C++ standard, polymorphism works for pointers and references to class objects.
(Looking up the standard for you will cost you more points)

I can't understand when this should be any problem, as you always can dereference a pointer to pointer
D** p;
myfn( *p );
ASKER CERTIFIED SOLUTION
Avatar of rcarlan
rcarlan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ponga_Pundit
Ponga_Pundit

ASKER

Sorry friemds, there was a little typo in my question. Please read the second last line as:

>> But if I change the myfn() parameter to A* ap, and change the call to myfn(pd), it works.
But still Radu's answer is fully acceptable.
>>But if I change the myfn() parameter to A**, and change the call to myfn(pd), it works. Why the polymorphism doesn't >>seem to work with double pointer?

did you mean myfn() parameter to A*, myfn(pd) ?
you put one more '*' there am i right?
i was late abit. cheers.