Blocking virtual methods
From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 09/29/04
- Next message: Default User: "Re: How to check whether my GCC compiler support C99 standard or not?"
- Previous message: Sumit Rajan: "Re: refrence from return value ?"
- Next in thread: Victor Bazarov: "Re: Blocking virtual methods"
- Reply: Victor Bazarov: "Re: Blocking virtual methods"
- Reply: Peter Kragh: "Re: Blocking virtual methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Sep 2004 16:45:14 GMT
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.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
- Next message: Default User: "Re: How to check whether my GCC compiler support C99 standard or not?"
- Previous message: Sumit Rajan: "Re: refrence from return value ?"
- Next in thread: Victor Bazarov: "Re: Blocking virtual methods"
- Reply: Victor Bazarov: "Re: Blocking virtual methods"
- Reply: Peter Kragh: "Re: Blocking virtual methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|