473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Please help understand virtual functions

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?
Jun 27 '08 #1
7 1118
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

}

<cc*******@gmail.comwrote in message
news:55**********************************@t54g2000 hsg.googlegroups.com...
>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?

Jun 27 '08 #2
Doesn't declaring a function a member of an interface imply that it's
virtual?
Jun 27 '08 #3
cc*******@gmail.com wrote:
Doesn't declaring a function a member of an interface imply that it's
virtual?
No.

--
Rudy Velthuis http://rvelthuis.de

"In this war – as in others – I am less interested in honoring
the dead than in preventing the dead." -- Butler Shaffer
Jun 27 '08 #4
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
Jun 27 '08 #5
Marc Gravell wrote:
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.
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)
Jun 27 '08 #6
On Wed, 23 Apr 2008 12:47:07 -0700 (PDT), cc*******@gmail.com wrote in
<55**********************************@t54g2000hsg. googlegroups.com>:
>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.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Jun 27 '08 #7
cc*******@gmail.com wrote:
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.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
3
by: CoolPint | last post by:
I read that the return type has to be exactly same for a virtual function to be overriden. While testing something, I discovered something I cannot understand. I cannot understand why the code...
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.