Blocking virtual methods

Discussion in 'C++' started by Thomas Matthews, Sep 29, 2004.

  1. 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
    Thomas Matthews, Sep 29, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Sep 29, 2004
    #2
    1. Advertising

  3. Thomas Matthews

    Peter Kragh Guest

    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
    Peter Kragh, Sep 30, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    12,344
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    898
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    802
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    714
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    244
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page