473,703 Members | 3,543 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 8843
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
7996
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
8670
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9254
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7872
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6592
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4433
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.