Blocking virtual methods

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 09/29/04


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


Relevant Pages

  • Any Grandparents out there?
    ... fields: GP PP1 CC1 ... I basically want to have CC1+CC2+CC3+CC4 show up on the Grandparent ... textbox in the Child form and then in the Parent form using something ...
    (microsoft.public.access.formscoding)
  • Re: Any Grandparents out there?
    ... I realized that I could eliminate the parent ... altogether and get the total that directly from the child table to the ... grandparent table. ... >> footer textbox in child to bring it up to Grandparent? ...
    (microsoft.public.access.formscoding)
  • Re: How to call the grandparents method
    ... you only have access to your parent. ... exist in your grandparent definition. ... > I am Child ... > public virtual void WhoAmI() ...
    (microsoft.public.dotnet.languages.csharp)
  • How to call the grandparents method
    ... I am Parent ... I am Child ... I am GrandParent ... public virtual void WhoAmI() ...
    (microsoft.public.dotnet.languages.csharp)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)