Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Consider this code

 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



output:-T2,T3,T2,T2,T3,T2
What is going on in this code?
Please help me with this.

Thanks
Sandhi
[ March 14, 2008: Message edited by: sandhi mridul ]
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is t2f() and t3f()?

Henry
 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Henry.
While typing I missed the imp thing -the dot operator.
I have corrected it.

Thanks
But still need the ans
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you do know that you are not overriding the f() method right? You are overloading it. With overloading, the type of reference is actually important -- as it is used to determine the method to call. And since the method is determined at compile time, the type of reference is important.

A T2 reference only has a single f() method, while a T3 reference has two overloaded f() methods. Meaning, it doesn't matter whether you pass a T2 or T3 reference to a T2 reference, it will be treated as a T2 reference. But it does matter whether you pass a T2 or T3 reference to a T3 reference, as it will be used to determine which overloaded method to call.

t2.f(t3); --> T2, because a T2 reference only has a single f() method
t3.f(t3); --> T3, because the overloaded f(T3) method is called
t2.f(t2); --> T2, because a T2 reference only has a single f() method
t3.f(t2); --> T2, because the overloaded f(T2) method is called
t3.f((T3)t2); --> T3, because the overloaded f(T3) method is called
t2.f((T3)t3); --> T2, because a T2 reference only has a single f() method

Henry
 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!