473,842 Members | 1,818 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
175 8928
> > Having read your clear example I say DBC can be implemented much,
much nicer and cleaner using events <snip>
No. It most definitely cannot. Private virtuals guarantees that the most
derived override is called once and once only when called from the base
class. Events do no such thing.


The point is to have the client implement alternative behavior. That is what
an event provides. I speak of client and not of derived class since
inheritence does not apply once we choose to use an event instead of a
sub-class to achieve our goal. You can still subclass and implement the new
behavior on any class-level you choose, that is the same as with the private
virtual method.

The (base) class would switch to either the default implementation (Carl's
virtual void part_b()) or the event implementation provided by the client,
depending on whether the event were implemented or not.

If the given example is the best reason to use private virtual methods, it
is really abusing polymorphism to implement an event mechanism. I am not
saying that polymorphism and events are equivalent, I am saying that the
example provided by Carl is a good example of when you should not use
polymorphism if you do have a more natural alternative like events
(delegates in C#).

Polymorphism and inheritence go hand in hand. If you want the polymorphism
part but you do not want the inheritence then you are really saying "this
mechanism is not really suited for my needs but that's okay, I'll cripple
the part that I don't need". It is like using an integer as a boolean.
:-))))))))).

Martin.
Nov 15 '05 #41

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:#3******** ******@TK2MSFTN GP10.phx.gbl...
The reason it damages design by contract is that it allows derived classes
to break the contract, since they can invoke their own virtual functions
without going through the public (contract-enforcing) interface.


Is it not already possible for derived classes to invoke those functions
anyways. It is a function that belongs to derived afterall? For example:

class Base
{
private:
virtual void f() = 0;
};

class Derived : public Base
{
public:
void g()
{ f(); }

private: // or whatever

virtual void f()
{}

};

I don't see how whether the base class having f() as private or protected
makes any difference to Derived? Additionally, whether 'f()' is protected or
private will make no difference to non-derived classes - it will be
inaccessable.

Whether 'f()' is protected or private _will_ make a difference if any
derived class tries to call 'f()' statically ( ie.. Base::f() ). If it was
private, it would fail.

Brian
Nov 15 '05 #42
One thing that the industry needs a clean up on is Ego and Arrogance.

Look at linux for a start.

"Brandon J. Van Every" <reverse it com dot indiegamedesign at vanevery>
wrote in message news:eL******** ******@TK2MSFTN GP11.phx.gbl...
n! wrote:
Because they're unnecessary thanks to the protected keyword. Why
support two ways to override a member when one access modifier has
semantics the other does not?

Considering nothing is virtual by default in C#, it makes perfect
sense to use protected virtual instead of private virtual.
The point about "private virtual" is that it allows child classes to
override the method but not call it, if the same method is defined
'protected virtual' then a derived class *can* call it. Whilst it may
seem esoteric, there are times when it's useful. Though TBH I've
never needed private virtual in C# so far.


It *is* esoteric, and I would wager that the C# language designers chose
simplicity in the interest of industrial robustness. "Industrial
robustness" in the real world means simplifying things so that the vast
majority of average programmers understand what's going on. C++ is a very
tweaky language with lotsa weird cases. C#, philosophically , is clearly

an attempt to clean up C++'s mess.

This observation may be lost on hardcore C++ programmers, and that is
precisely the point. In the long haul, C# will replace C++ for an awful lot of application programming tasks. It has already happened at Microsoft and it's only a matter of time for it to happen with the vast majority of
Windows development.

--
Cheers, www.indiegamedesign.com
Brandon Van Every Seattle, WA

20% of the world is real.
80% is gobbledygook we make up inside our own heads.

Nov 15 '05 #43
You call having to DUPLICATE method signitures (prototypes) as good coding?
Its to make up for the bad design in the compilers.

These days our Tools work for us not we work for them. Welcome to the REAL
world.
"Andreas Huber" <ah****@gmx.net > wrote in message
news:40******** **@news.bluewin .ch...
Brandon J. Van Every wrote:
n! wrote:
Because they're unnecessary thanks to the protected keyword. Why
support two ways to override a member when one access modifier has
semantics the other does not?

Considering nothing is virtual by default in C#, it makes perfect
sense to use protected virtual instead of private virtual.

The point about "private virtual" is that it allows child classes to
override the method but not call it, if the same method is defined
'protected virtual' then a derived class *can* call it. Whilst it may
seem esoteric, there are times when it's useful. Though TBH I've
never needed private virtual in C# so far.


It *is* esoteric, and I would wager that the C# language designers
chose simplicity in the interest of industrial robustness.


You seem to suggest that C++s support for private virtual functions makes
C++ less robust. Funny, I'd claim exactly the opposite.
"Industrial robustness" in the real world means simplifying things so
that the vast majority of average programmers understand what's going
on.


Simple: If you don't understand it, don't use it and everything will be
well. If you happen to maintain somebody else's code using the private
virtual idiom it should become obvious very quickly how it works. If not,
post an appropriate question to comp.lang.c++ ;-).
I really fail to see how private virtuals hurt "industrial robustness".

Regards,

Andreas

Nov 15 '05 #44
> You seem to suggest that C++s support for private virtual functions makes
C++ less robust. Funny, I'd claim exactly the opposite.
"Industrial robustness" in the real world means simplifying things so
that the vast majority of average programmers understand what's going
on.


Simple: If you don't understand it, don't use it and everything will be
well. If you happen to maintain somebody else's code using the private
virtual idiom it should become obvious very quickly how it works. If not,
post an appropriate question to comp.lang.c++ ;-).
I really fail to see how private virtuals hurt "industrial robustness".


The general point Brandon is making is that in C++ you will easily "use" a
lot of nifty features that you do not understand without being aware of it,
it is unfornunately not a choice in many cases (no pun intended). That has
been acknowledged by the C# designers.

In the private virtual discussion, robustness may not be an issue. I would
call it "not elegant".
Every time something new comes along the established lot will say "Kid's
stuff, no gain, too slow, don't need it". And after a while we all learn to
appreciate it.

And oh (I almost forgot): "my language is better than yours".

Martin.
Nov 15 '05 #45
Brian Ross wrote:
"Carl Daniel [VC++ MVP]"
<cp************ *************** **@mvps.org.nos pam> wrote in message
news:#3******** ******@TK2MSFTN GP10.phx.gbl...
The reason it damages design by contract is that it allows derived
classes to break the contract, since they can invoke their own
virtual functions without going through the public
(contract-enforcing) interface.


Is it not already possible for derived classes to invoke those
functions anyways. It is a function that belongs to derived afterall?


D'oh! you're right of course.

The derived class can still break the contract for itself. Perhaps then the
only advantage to having the methods private instead of protected is to
inform the person who's writing a derived class that the method shouldn't be
called at all (except by the existing, public methods in the base class).

Mea culpa.

-cd
Nov 15 '05 #46

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:en******** ******@TK2MSFTN GP10.phx.gbl...
The derived class can still break the contract for itself. Perhaps then the only advantage to having the methods private instead of protected is to
inform the person who's writing a derived class that the method shouldn't be called at all (except by the existing, public methods in the base class).


Perhaps.

Personally, for what little difference it actually makes in practice, I have
started using the convention that anything virtual is protected or public
(never private).

My reasoning is this:

'public' means anything that is important to anyone.
'protected' means anything that is important only to derived classes and
should not be made public.
'private' is anything that is important only to the class itself.

Because 'virtual' is always a factor for derived classes - this means (to
me) that it should be classified as protected (or public).

[The outcome of this is that when I am looking at a class interface inside a
header file, I can limit myself to either public items (if I am just using
the class) or public/protected items (if I am deriving from the class).]

I suspect this is similar to the reasoning behind how C#/java work.

Brian
Nov 15 '05 #47
Andreas Huber wrote:
Brandon J. Van Every wrote:

Simple: If you don't understand it, don't use it and everything will
be well. If you happen to maintain somebody else's code using the
private virtual idiom it should become obvious very quickly how it
works. If not, post an appropriate question to comp.lang.c++ ;-).
I really fail to see how private virtuals hurt "industrial
robustness".


Keep It Simple Stupid. If you don't understand how that affects industrial
robustness, you are a C++ tweak-head. People get paid looooooootsa money to
understand each and every one of C++'s weirdnesses.

--
Cheers, www.indiegamedesign.com
Brandon Van Every Seattle, WA

20% of the world is real.
80% is gobbledygook we make up inside our own heads.

Nov 15 '05 #48
.. wrote:
One thing that the industry needs a clean up on is Ego and Arrogance.
Why?
Look at linux for a start.


Is that an example of more or less ego and arrogance? And is that resulting
in better or worse something or other?

--
Cheers, www.indiegamedesign.com
Brandon Van Every Seattle, WA

20% of the world is real.
80% is gobbledygook we make up inside our own heads.

Nov 15 '05 #49
Carl Daniel [VC++ MVP]
<cp************ *************** **@mvps.org.nos pam> wrote:
It's clearly simpler, and it may well be easier to understand (personally I
din't think so, but then I'm from a C++ background).

The significance of this thread though is to amplify the point that this
simplification comes with a cost: certain very valuable design idioms are
simply not possible in C# (or Java) because of the restriction on overriding
a private method.


It appears later on that it doesn't actually give a valuable design
idiom, because the method can be called by the derived class anyway. I
suppose it stops it from being called by a class which is *further*
derived.

Anyway, the C++ approach has a cost too: private in C++ being not as
private as in C#/Java comes at the cost of namespace pollution. One of
the nice things about private methods etc is that whatever you call
them when you first write them, you can change that name later with
*no* impact to other classes (that aren't doing nasty reflection
things). No-one else knows about them, because they're private. (Even
the word "private" doesn't ring true with the C++ semantics, IMO.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #50

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

Similar topics

164
8069
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
9867
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9709
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
10308
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9449
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
7853
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
7028
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5695
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...
1
4498
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
2
4086
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.