• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About polymorphism

 
Jack Lau
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell me why compiler show the following error message ?
"reference to m is ambiguous, both method m(B, B) in C and method m(A, C) in C match m(c, c)."

But if I change m(A x, C y) to m(B x, C y) will work properly.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question was just answered within the last few days.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try, but I do not feel 100% that I understand it myself; so beware.
The compiler must try to decide which of m(B, B) or m(A, C) is "most specific" to the actual call which is type m(C, C). If we choose the m(B, B) case we can object because the second argument is a C, so m(A, C) is "more specific". If we choose the m(A, C) case we can object because the first argument is a C which is a B, so m(B, B) is "more specific". Because we can object in both cases it cannot be decided which of the two methods to call.
When you change the m(A, C) to m(B, C) we can choose m(B, C) because the second argument is a C which is more "specific" than it being a B.
Is there some easy algorithm for resolving these things?
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Thomas means here.
 
Jack Lau
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
I think I got what you mean.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic