473,748 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Blocking virtual methods

Hi,

In pursuit of a child class blocking a parent's virtual method,
I came up with this example:

#include <iostream>
#include <cstdio>
using namespace std;

class Grandparent
{
public:
virtual void who_am_i(void)
{ cout << "I am grandparent." << endl;}
};
class Parent
: public Grandparent
{
public:
virtual void who_am_i(void)
{ cout << "I am parent." << endl;}
};
class Child
: public Parent
{
private:
void who_am_i(void)
{ cout << "I am child." << endl;}
};
int main(void)
{
Grandparent gp;
Grandparent * person;
Parent p;
Child c;

cout << "gp: ";
gp.who_am_i();
cout << "p: ";
p.who_am_i();
person = &c;
cout << "\n Person{child}: ";
person->who_am_i();

return EXIT_SUCCESS;
}

I placed the above text into junk.cpp, built and
executed it:
$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -o junk junk.cpp

$ ./junk
gp: I am grandparent.
p: I am parent.

Person{child}: I am child.
Does this make sense that a virtual method that is declared
private can still be executed using pointers?

I would like to have a child convert a parent virtual
method into something that "is not supported". My current
route is to use:

#include <stdexcept>
class Child
: public Parent
{
public:
void who_am_i(void)
{ throw runtime_error(" who_am_i() not supported for child."); }
};

I want to block some virtual parental methods, but not all
of them. Any suggests for a better alternative to block
a parental public virtual method (so that not any of the
ascendants methods are executed)?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #1
2 1640
Thomas Matthews wrote:
[...]
I want to block some virtual parental methods, but not all
of them. Any suggests for a better alternative to block
a parental public virtual method (so that not any of the
ascendants methods are executed)?


This mechanism does not exist in C++. Please read the discussions
on "final" in the news archives.

In most cases, IIRC, if you think you need to "block" some behaviour
defined in the base class, your design is flawed. Use containment
and delegation instead of public inheritance.

Victor
Jul 22 '05 #2
Thomas Matthews wrote:

<snip>
class Child
: public Parent
{
private:
void who_am_i(void)
{ cout << "I am child." << endl;}
};
int main(void)
{
Grandparent gp;
Grandparent * person;
Parent p;
Child c;

cout << "gp: ";
gp.who_am_i();
cout << "p: ";
p.who_am_i();
person = &c;
cout << "\n Person{child}: ";
person->who_am_i();

return EXIT_SUCCESS;
}
<snip>
Does this make sense that a virtual method that is declared
private can still be executed using pointers?

The access specifier used when calling functions using a pointer or
reference is determined by the /static/ type. In this case: class
Grandparent.

Declaring it private in the Child class only means that you cannot
invoke it directly on objects of this class.
I would like to have a child convert a parent virtual
method into something that "is not supported". My current
route is to use:

#include <stdexcept>
class Child
: public Parent
{
public:
void who_am_i(void)
{ throw runtime_error(" who_am_i() not supported for child."); }
};

I want to block some virtual parental methods, but not all
of them. Any suggests for a better alternative to block
a parental public virtual method (so that not any of the
ascendants methods are executed)?


Since you cannot block virtual methods, you need to figure out what
should happen, if someone calls the "blocked" method anyway. This
depends on the problem you are trying to solve. In this case, why not
just make an empty function.

HTH
Peter
Jul 22 '05 #3

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

Similar topics

20
1781
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.
3
12260
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
32
4524
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
12142
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...
175
8876
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....
5
15851
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 } }
3
3289
by: Bob | last post by:
Hi, I have an app that has a 3rd party phone answering control (4 of ) (interfacing with dialogic 4 line card) attached to the main form. each control raises an event when its Dialogic line detects ring tone. I use the ring detect event handler to create a new thread which is given a reference to the control that is being rung. The called method then interacts with the control until the call finishes. My problem is that I believe I am...
318
11059
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++?
0
8996
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
9562
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
9386
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
9254
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
6078
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
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.