Blocking virtual methods

T

Thomas Matthews

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
 
V

Victor Bazarov

Thomas said:
[...]
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
 
P

Peter Kragh

Thomas Matthews wrote:

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;
}

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top