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. 11 1986
"Dave Rudolf" <nu************ ****@hotmail.co m> 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?
"Dave Rudolf" <nu************ ****@hotmail.co m> wrote in message
news:10******** *****@corp.supe rnews.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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:TSiXb.3021 23$I06.3076461@ attbi_s01... "Dave Rudolf" <nu************ ****@hotmail.co m> 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.
"Dave Rudolf" <nu************ ****@hotmail.co m> wrote... "Victor Bazarov" <v.********@com Acast.net> wrote in message news:TSiXb.3021 23$I06.3076461@ attbi_s01... "Dave Rudolf" <nu************ ****@hotmail.co m> 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
> > 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.
"unintentio nal 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.
"Dave Rudolf" <nu************ ****@hotmail.co m> 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.
"unintentio nal 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
On Sat, 14 Feb 2004 11:39:01 -0600 in comp.lang.c++, "Dave Rudolf"
<nu************ ****@hotmail.co m> 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++.
"Dave Rudolf" <nu************ ****@hotmail.co m> wrote in message news:<10******* ******@corp.sup ernews.com>... "Victor Bazarov" <v.********@com Acast.net> wrote in message news:TSiXb.3021 23$I06.3076461@ attbi_s01... "Dave Rudolf" <nu************ ****@hotmail.co m> 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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:E6zXb.3062 44$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. "unintentio nal 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Perforamce Tips and Tricks in .NET Applications, A .Net
Developer Platform White Paper, at the very bottom, it says:
The JIT cannot inline virtual methods, so you lose a potential optimization
if you get rid of non-virtual methods.
|
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
|
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years
namespace MyNamespac
public delegate void MyDel()
public class MyBase
public virtual...
|
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....
|
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
}
}
| |
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 ...
http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html
Just to keep the post on topic for my friends at comp.lang.c++, how do
I play default windows sounds with C++?
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
| |
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |