473,468 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Finalizing virtual methods

Hi all,

Suppose that I have a class (let's call the class A) with a virtual method.
I then define a subclass (B), which could potentially be extended by some
other subclass (say, C). So, A <- B <- C. I want to prevent C from
redefining the virtual method in same manner as how a "final" method in Java
cannot be overridden.. Is there some way to do this in C++?

Dave.
Jul 22 '05 #1
11 1964
"Dave Rudolf" <nu****************@hotmail.com> wrote...
Suppose that I have a class (let's call the class A) with a virtual method. I then define a subclass (B), which could potentially be extended by some
other subclass (say, C). So, A <- B <- C. I want to prevent C from
redefining the virtual method in same manner as how a "final" method in Java cannot be overridden.. Is there some way to do this in C++?


No. Why would you want to do that?
Jul 22 '05 #2

"Dave Rudolf" <nu****************@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
Hi all,

Suppose that I have a class (let's call the class A) with a virtual method. I then define a subclass (B), which could potentially be extended by some
other subclass (say, C). So, A <- B <- C. I want to prevent C from
redefining the virtual method in same manner as how a "final" method in Java cannot be overridden.. Is there some way to do this in C++?

Dave.


Documentation, its the only way. Or choose another language, perhaps
beginning with the letter J.

john
Jul 22 '05 #3

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:TSiXb.302123$I06.3076461@attbi_s01...
"Dave Rudolf" <nu****************@hotmail.com> wrote...
Suppose that I have a class (let's call the class A) with a virtual

method.
I then define a subclass (B), which could potentially be extended by some other subclass (say, C). So, A <- B <- C. I want to prevent C from
redefining the virtual method in same manner as how a "final" method in

Java
cannot be overridden.. Is there some way to do this in C++?


No. Why would you want to do that?


Security. In my case, B requires the virtual method to behave in a certain
way. If C overrides the method, it could break the expected behavior.
Jul 22 '05 #4
"Dave Rudolf" <nu****************@hotmail.com> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:TSiXb.302123$I06.3076461@attbi_s01...
"Dave Rudolf" <nu****************@hotmail.com> wrote...
Suppose that I have a class (let's call the class A) with a virtual

method.
I then define a subclass (B), which could potentially be extended by some other subclass (say, C). So, A <- B <- C. I want to prevent C from
redefining the virtual method in same manner as how a "final" method
in Java
cannot be overridden.. Is there some way to do this in C++?


No. Why would you want to do that?


Security. In my case, B requires the virtual method to behave in a certain
way. If C overrides the method, it could break the expected behavior.


Then your 'B' class cannot be derived from (or your supposition that
it "could potentially be extended" is incorrect). To make 'B' non-
derivable, declare its constructor[s] private.

Otherwise, just document the behaviour and your requirement not to have
it overridden. If somebody willingly implements the overrider in 'C'
class after you told them not to, it's not your responsibility.

C++ does not have any means to prohibit anything, only to protect
from unintentional errors. I am fairly sure that if you are in
control of 'A' and 'B' classes, you can change their interface or the
implementation so that the behaviour that needs to be hidden is, in
fact, simply hidden.

BTW, the 'final' specifier in Java is not something they should be
proud of, IMO.

Victor
Jul 22 '05 #5
> >
Security. In my case, B requires the virtual method to behave in a certain way. If C overrides the method, it could break the expected behavior.
Then your 'B' class cannot be derived from (or your supposition that
it "could potentially be extended" is incorrect). To make 'B' non-
derivable, declare its constructor[s] private.


Ah, but there may still be need to inherit from that class, either to merely
gain its functionality (which is not really good coding practice -- leads to
spagetti inheritance) or because it is overriding other methods.

C++ does not have any means to prohibit anything, only to protect
from unintentional errors. I am fairly sure that if you are in
control of 'A' and 'B' classes, you can change their interface or the
implementation so that the behaviour that needs to be hidden is, in
fact, simply hidden.

"unintentional errors" can be defined rather braodly.
BTW, the 'final' specifier in Java is not something they should be
proud of, IMO.

I see it as no different than type checking or the const modifier: that is
to have the compiler prevent the coder from doing something that they
shouldn't be doing.
Victor


Dave.
Jul 22 '05 #6
"Dave Rudolf" <nu****************@hotmail.com> wrote...

Security. In my case, B requires the virtual method to behave in a certain way. If C overrides the method, it could break the expected behavior.
Then your 'B' class cannot be derived from (or your supposition that
it "could potentially be extended" is incorrect). To make 'B' non-
derivable, declare its constructor[s] private.


Ah, but there may still be need to inherit from that class, either to

merely gain its functionality (which is not really good coding practice -- leads to spagetti inheritance) or because it is overriding other methods.
Again, if you intend to override other member functions, then all
member functions declared virtual are fair game. If you don't
want the programmer deriving from your file to override certain
methods, _all_you_have_ is documentation. Simple as that. No
need to discuss it any further. There is no 'final' (or any of
its potential analoques) in C++.

C++ cannot prevent people from doing stupid things. It's rather
impossible in the language which is already as complex as C++ is.
And attempting to "simplify" it in certain areas can lead to...
....well, Java.
C++ does not have any means to prohibit anything, only to protect
from unintentional errors. I am fairly sure that if you are in
control of 'A' and 'B' classes, you can change their interface or the
implementation so that the behaviour that needs to be hidden is, in
fact, simply hidden.


"unintentional errors" can be defined rather braodly.


OK, errors are almost always unintentional, I'll give you that. But
what else do you have against my statement? There is no way in hell
to make your compiler not let you, say, access class private members
if you have access to the class header, just add your 'friend'
statement there, for example. There is no way to protect from never
'delete'ing a pointer that was allocated using 'new' (although some
did implement a garbage collector for C++), or from 'delete'ing the
same pointer twice. Sacrifices have to be made. That's why machines
writing programs in C++ are still losing in numbers to people.
BTW, the 'final' specifier in Java is not something they should be
proud of, IMO.


I see it as no different than type checking or the const modifier: that is
to have the compiler prevent the coder from doing something that they
shouldn't be doing.


I am not going to discuss Java in a C++ newsgroup. Do you have any
other C++ question?

V
Jul 22 '05 #7
On Sat, 14 Feb 2004 11:39:01 -0600 in comp.lang.c++, "Dave Rudolf"
<nu****************@hotmail.com> was alleged to have written:
Security. In my case, B requires the virtual method to behave in a certain
way. If C overrides the method, it could break the expected behavior.


AKA design by contract. Some people get one level of it by making all
virtual functions private, and called by a non-virtual function in the
base class that enforces the pre- and post-conditions expected. Hard to
cover the whole inheritance hierarchy that way, though. Maybe one level
would be some help in your case.

Full language support for design by contract is one of the often
requested features for a future version of C++.
Jul 22 '05 #8
"Dave Rudolf" <nu****************@hotmail.com> wrote in message news:<10*************@corp.supernews.com>...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:TSiXb.302123$I06.3076461@attbi_s01...
"Dave Rudolf" <nu****************@hotmail.com> wrote...
Suppose that I have a class (let's call the class A) with a virtual method. I then define a subclass (B), which could potentially be extended by some other subclass (say, C). So, A <- B <- C. I want to prevent C from
redefining the virtual method in same manner as how a "final" method in Java cannot be overridden.. Is there some way to do this in C++?


No. Why would you want to do that?


Security. In my case, B requires the virtual method to behave in a certain
way. If C overrides the method, it could break the expected behavior.


If B requires the virtual method to behave in a certain way, then why
is it virtual? I'm not suggesting a case like this couldn't possibly
exist, but I'm curious in the exact case that concerns you.

Dylan
Jul 22 '05 #9

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:E6zXb.306244$I06.3136045@attbi_s01...

Again, if you intend to override other member functions, then all
member functions declared virtual are fair game. If you don't
want the programmer deriving from your file to override certain
methods, _all_you_have_ is documentation. Simple as that. No
need to discuss it any further. There is no 'final' (or any of
its potential analoques) in C++.

Okay, so I understand what I want to do cannot be done in C++, and yes that
the need for such a feature is a relatively rare. The discussion has moved
to the more philosphical note below.
C++ cannot prevent people from doing stupid things. It's rather
impossible in the language which is already as complex as C++ is.
And attempting to "simplify" it in certain areas can lead to...
...well, Java.

Of course, a compiler cannot read the mind of the coder, and too much
compiler intervension can restrict what the coder can do (e.g. garbage
collection leads to wasted CPU at the expense of coding ease). But, that
does not mean that we should give up on trying to help the coder. Especially
when systems are getting larger and more complex, with larger development
teams, programmers need all the help that they can get. What happens in a
natural language when some new thing arises in society? We evolve the
language to adequately differentiate between the new thing and what was
there before. C++ evolves very slowly, which is good -- it doesn't
incorporate every trendy feature that comes along. But, as far as the ANSI
language goes, it has been rather stagnant the last decade or so. Perhaps it
is time to start moving again.

"unintentional errors" can be defined rather braodly.


OK, errors are almost always unintentional, I'll give you that. But
what else do you have against my statement? There is no way in hell
to make your compiler not let you, say, access class private members
if you have access to the class header, just add your 'friend'
statement there, for example. There is no way to protect from never
'delete'ing a pointer that was allocated using 'new' (although some
did implement a garbage collector for C++), or from 'delete'ing the
same pointer twice. Sacrifices have to be made. That's why machines
writing programs in C++ are still losing in numbers to people.


I guess what I am arguing is a matter of taste. One person's unintentional
error is another person's regular playing ground.

I see it as no different than type checking or the const modifier: that is to have the compiler prevent the coder from doing something that they
shouldn't be doing.


I am not going to discuss Java in a C++ newsgroup. Do you have any
other C++ question?


I think it's fair to bring up other languages in comparison to the current
one. Granted, I know that it is taboo to say the J-word in this group :).
V


Dave.
Jul 22 '05 #10

"David Harmon" <so****@netcom.com> wrote in message news:40****************@news.west.earthlink.net...
On Sat, 14 Feb 2004 11:39:01 -0600 in comp.lang.c++, "Dave Rudolf"
<nu****************@hotmail.com> was alleged to have written:
Security. In my case, B requires the virtual method to behave in a certain
way. If C overrides the method, it could break the expected behavior.


AKA design by contract. Some people get one level of it by making all
virtual functions private, and called by a non-virtual function in the
base class that enforces the pre- and post-conditions expected. Hard to
cover the whole inheritance hierarchy that way, though. Maybe one level
would be some help in your case.


I don't know if this helps this at all. Access control doesn't have any effect
on overriding. Even private virtual methods can be overridden.

Jul 22 '05 #11
On Tue, 17 Feb 2004 12:31:50 -0500 in comp.lang.c++, "Ron Natalie"
<ro*@sensor.com> wrote:

"David Harmon" <so****@netcom.com> wrote in message news:40****************@news.west.earthlink.net...
AKA design by contract. Some people get one level of it by making all
virtual functions private, and called by a non-virtual function in the
base class that enforces the pre- and post-conditions expected. Hard to
cover the whole inheritance hierarchy that way, though. Maybe one level
would be some help in your case.


I don't know if this helps this at all. Access control doesn't have any effect
on overriding. Even private virtual methods can be overridden.


Yes, that's the point. You still want derived classes to be able to
override the virtual function. You just want to restrict the behavior
of the overriding function. The base class restricts it by checking
preconditions, calling the virtual, then checking postconditions, all in
a public non-virtual function. Foreign classes cannot call the virtual
directly, but only via the base class interface.

That may not be everything the O.P. was asking for, but it's a start.

Jul 22 '05 #12

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

Similar topics

20
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
175
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...
5
by: ^MisterJingo^ | last post by:
Is it good pratice to make base class properties virtual? For example: public abstract class Person { protected string name; public virtual string Name { //get, set stuff here } }
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
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
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.