[Next] [Prev] [Up] [Top]

7.0 Classes

Do not provide a non-virtual method in a derived class by the same name as an inherited method. (R)


The effect of non-virtual methods in a base and derived class by the same name is the introduction of subtle behavior that depends on the static type of the instance pointer. This is a bad thing.

The only exception is using methods like these which are used only to return a return type of a more derived type.

class X {

public:

void bar () { writeFile(); }

X* ok () { return findParent();}

};

class Y : public X {

public:

void bar () { ringBell(); }

Y* ok () { return (Y*) X::ok(); }

};

void foo ()

{

Y* yp= new Y; // one instance of Y

X* xp = yp; // point to same instance as an X

Y* foundY = yp->ok (); // ok

X* foundX = xp->ok (); // ok

xp->bar (); // ok, file written

xp->bar (); // surprise! your bell was rung!

}


No Title
[Next] [Prev] [Up] [Top]

Generated with CERN WebMaker

This site was grabbed using the TRIAL version of Grab-a-Site. This message does not appear on a licensed copy of Grab-a-Site.