#What actually is dynamic method dispatch?

14 messages · Page 1 of 1 (latest)

past oyster
#

What is the real need of taking reference variable of base class in dynamic method dispatch
https://pastebin.com/W3DQvsfg
the code here implements Dynamic method dispatch but my question is instead of taking base class reference as A r; can we just create object of respective class and call the function? like just do: ```
a.m1();
b.m1();
c.m1();

so what is significance of ``` A r;```???
crimson dewBOT
#

This post has been reserved for your question.

Hey @past oyster! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

alpine ferry
#

I'm assuming the reference is taken just to demonstrate that even though reference is of Parent class, overriden method remains dominant

past oyster
alpine ferry
#

When you do A#m1() you get A'm1

#

But when you put a C object in an A variable,

#

C#m1() gives you the C's m1

#

But say you have a method m2 in C but not in A

#

Then from the other variable you cannot call the m2 method

past oyster
#

okk

#

means it is the type of the object being referred to ,not the type of the
reference variable, bcz if it is the other way around it would only call func of A but infact it is calling fun based on the object being reffred

#

Thnx @alpine ferry for the explanation.