473,581 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"All public methods should be virtual" - yes or no / pros & cons

I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual"
just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?

Thanks in advance....
Nov 15 '05 #1
175 8757
If you plan on them to be derived from then yes obviously.

If its a sealed class, then its pointless and the compiler (if it doesnt)
should prevent having virtuals in a sealed class.

"Ken Brady" <Ke***********@ thomson.com> wrote in message
news:O$******** ******@TK2MSFTN GP12.phx.gbl...
I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?

Thanks in advance....

Nov 15 '05 #2
"Ken Brady" <Ke***********@ thomson.com> skrev i meddelandet
news:O$******** ******@TK2MSFTN GP12.phx.gbl...
I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?


In fact, most experts think the opposite: "No public member functions should
be virtual".

A class exposes two interfaces, a calling interface (public functions) and a
derivation interface (virtual functions). They have different purpose and
different users and should be separate.

--
Dag Henriksson
Nov 15 '05 #3
There are very good reasons for not making a method virtual unless it needs
to be. That's why virtual is not the default in C#.
Here's Anders Hejlsberg's( the lead C# architect ) take:
http://www.artima.com/intv/nonvirtual.html

/Magnus Lidbom

"Ken Brady" <Ke***********@ thomson.com> wrote in message
news:O$******** ******@TK2MSFTN GP12.phx.gbl...
I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?

Thanks in advance....


Nov 15 '05 #4
Not public no, protected only.should be virtual in my view.

Does other languages prevent PUBLIC virtuals and only allow protected
virtuals?
"Dag Henriksson" <da************ @hotmail.com> wrote in message
news:bv******** ****@ID-200546.news.uni-berlin.de...
"Ken Brady" <Ke***********@ thomson.com> skrev i meddelandet
news:O$******** ******@TK2MSFTN GP12.phx.gbl...
I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual"
just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me?


In fact, most experts think the opposite: "No public member functions

should be virtual".

A class exposes two interfaces, a calling interface (public functions) and a derivation interface (virtual functions). They have different purpose and
different users and should be separate.

--
Dag Henriksson

Nov 15 '05 #5
n!
> If its a sealed class, then its pointless and the compiler (if it doesnt)
should prevent having virtuals in a sealed class.


FWIW the compiler will emit an error if you try to declare a new virtual
method within a sealed class (it will also give a warning about new
protected methods in a sealed class).

n!
Nov 15 '05 #6
"Ken Brady" <Ke***********@ thomson.com> wrote in message
news:O$******** ******@TK2MSFTN GP12.phx.gbl...
I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?

Thanks in advance....

Mmmm.

I prefer the statement, "all virtual functions should be private", or the
milder "all virtual functions should at least be protected". That does not
include the destructor.

But that does not rule out public virtual functions.

Only functions that were designed to be virtual, should be virtual.

Tom.
Nov 15 '05 #7

<di********@dis cussion.microso ft.com> skrev i meddelandet
news:Og******** ******@TK2MSFTN GP10.phx.gbl...
Not public no, protected only.should be virtual in my view.


I prefer ususally 'private virtual', and I only use 'protected virtual' if I
have a good reason to.

--
Dag Henriksson
Nov 15 '05 #8
Dag Henriksson wrote:
I prefer ususally 'private virtual', and I only use 'protected
virtual' if I have a good reason to.


A private method cannot be derived so what's the point of marking it
virtual?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #9
Frank Oquendo wrote:
Dag Henriksson wrote:
I prefer ususally 'private virtual', and I only use 'protected
virtual' if I have a good reason to.


A private method cannot be derived so what's the point of marking it
virtual?


A private method most certainly CAN be overridden in a derived class. It
simply can'y be CALLED from outside the class where it's first declared.

In the "private virtual" paradigm, an abstract base class exposes a
non-virtual public interface. These non-virtual functions check and inforce
the invariants of the class's interface (look up "design by contract" if
that doesn't ring a bell), and delegate to private virtual methods to
perform the "meat" of the operations. Derived classes can override the
virtual methods to tune the behavior of the class, but since only the base
class's non-virtual interface is public, everyone, including derived
classes, must go through the base interface to access the class (ensuring
that there are no holes in the invariant checking).

-cd
Nov 15 '05 #10

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

Similar topics

164
7952
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
0
7882
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6564
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5683
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.