473,699 Members | 2,907 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 1636
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
1774
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
12240
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
4512
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
12132
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
8810
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
15850
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
3286
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
10999
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
8704
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
8623
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
9054
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
8894
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
7777
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
6544
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
4390
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
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2361
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.