Early/late binding

  • Thread starter Thread starter joe
  • Start date Start date
J

joe

Hi,

In C#, objects are manipulated by their references only.
Does that mean that all calls to virtual functions are
resolved at run time?
Can a call to a virtual function be resolved at compile
time

Thanks
 
joe said:
In C#, objects are manipulated by their references only.
Does that mean that all calls to virtual functions are
resolved at run time?
Yes.

Can a call to a virtual function be resolved at compile time

How would you expect that to work, out of interest? What kind of effect
would you want?
 
I don't think I can answer your question exactly, but there IS a look-up for
all virtual functions at run-time. I think this may only happen once
though, kind of like filling in a v-table pointer. You'll find that a lot
of interesting CLR methods aren't marked virtual so you can play with them
for this exact reason. They want the maximum speed and so don't want the
overhead of have a virtual function look-up when the method is called.
 
joe said:
Hi,

In C#, objects are manipulated by their references only.
Does that mean that all calls to virtual functions are
resolved at run time?
Can a call to a virtual function be resolved at compile
time

If the optimizer is clever enough, it can do some compile-time resolution,
e.g

MyClass obj = new MyClass();
obj.CallVirtual();
 
Hi,
Well, I was just trying to compare with C++, where if a
virtual function is called through an object (not a
pointer to it), it is not a virtual call

This is a little trickier with .NET because, like you said, anything with
virtual methods must be a class (and therefore accessed by reference). I've
heard (from the thread about non-virtual methods being default) that Java
can do something like your optimisation.. so I guess it could make it into
the JIT at some point.

Is there some specific reason you need the extra performance? I've found
that in practice, it's a rare situation that requires it. Although it's
always nice to be optimal.

Pete
 
Back
Top