Please help understand virtual functions

C

cctv.star

I have a base class that implements IDisposable.

Then I have a derived class, in which I write the following:

public override void Dispose() {
}

... only to get an error: cannot override inherited member ...Dispose()
because it is not marked virtual, abstract, or override. Why??? How a
function, implementing an interface, not be virtual?

And how can I do what I want?
 
M

Mike Urquiola

Just mark the base method as virtual



public class MyClass:IDisposable {

#region IDisposable Members

public virtual void Dispose() {

throw new NotImplementedException();

}

#endregion

}

public class MyClass2 : MyClass {

#region IDisposable Members

public override void Dispose() {

base.Dispose();

throw new NotImplementedException();

}

#endregion

}
 
M

Marc Gravell

No.

Well, that's a bit of an over-simplification; from the implementing
class's perspective (the callee) - then indeed it isn't automatically
virtual, and you can't override etc; but from the caller's
perspective, it is a virtcall, since it can't possibly static-bind to
an interface method, and the JIT won't ever attempt to inline etc.

Marc
 
R

Rudy Velthuis

Marc said:
Well, that's a bit of an over-simplification; from the implementing
class's perspective (the callee) - then indeed it isn't automatically
virtual, and you can't override etc; but from the caller's
perspective, it is a virtcall, since it can't possibly static-bind to
an interface method, and the JIT won't ever attempt to inline etc.

Sure, calling an interface method is similar to calling a virtual
method (the internal mechanism is very similar, indeed). But the method
is not necessarily virtual in the implementing class.
--
Rudy Velthuis http://rvelthuis.de

"All truth passes through three stages. First, it is ridiculed.
Second, it is violently opposed. Third, it is accepted as being
self-evident."
-- Arthur Schopenhauer (1788-1860)
 
C

Charles Calvert

I have a base class that implements IDisposable.

Then I have a derived class, in which I write the following:

public override void Dispose() {
}

.. only to get an error: cannot override inherited member ...Dispose()
because it is not marked virtual, abstract, or override. Why?

Are you asking why you got the error?
How a function, implementing an interface, not be virtual?

An interface is a list of methods and properties that you must
implement. You're not inheriting from a class, just making your class
conform to a standard. There is no base implementation of Dispose to
override.
And how can I do what I want?

Look at the docs for IDisposable.Dispose. There is plenty of
information and an example.
 
B

Ben Voigt [C++ MVP]

I have a base class that implements IDisposable.

Then I have a derived class, in which I write the following:

public override void Dispose() {
}

.. only to get an error: cannot override inherited member ...Dispose()
because it is not marked virtual, abstract, or override. Why??? How a
function, implementing an interface, not be virtual?

And how can I do what I want?

You could also implement IDisposable in the derived class, this
implementation will override the base class. However if the base class did
an explicit override, you will not be able to invoke it from your derived
class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top