[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: rule of thumb on when to make a method virtual?
From:       Navindra Umanee <navindra () cs ! mcgill ! ca>
Date:       1998-05-09 17:44:48
[Download RAW message or body]

Montreal Sat May  9 13:31:00 1998

At one point in cyberspacetime,
Preston Brown <pbrown@econ.yale.edu> wrote:

> Do you guys have a rule of thumb on when to make your method virtual?  Is
> it pretty much just whenever you think that someone might override it

I'd probably always make it virtual.  Probably less efficient than
non-virtual but nicer behavior.  It might be interesting to note here 
that Java methods have the virtual behavior by default.

> through inheritance?  Does someone have a *concise* summary of when a
> non-virtual method *Doesn't* get called in a child class, but in the
> parent class instead?  I have read so many c++ books, my mind is sort of
> hazy and I am not sure I always make the right decisions on this stuff.

- Child inherits Parent and overrides non-virtual method blah()

Child *object1;
Parent *object2;

object1 = new Child();

// Child's version of blah() gets called here.
object1->blah();

// object2 is simply object1 typecasted from Child to Parent!
object2 = (Parent*) object1;

// Parent's version of blah() gets called here.
object2->blah();

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic