Re: Something like a final method
On Mar 21, 8:40 pm, blargg....@gishpuppy.com (blargg) wrote:
=?ISO-8859-1?Q?Marcel_M=FCller?= wrote:
class Interface
{public:
virtual int GetStatus() = 0;
};
class AbstractBase : public Interface
{private:
int Status;
public:
virtual int GetStatus() { return Status; } // Never overridden
};
If I call GetStatus() through a pointer to AbstractBase
(or any derived class) the call cannot be inlined
because it is a virtual function. Of course, there is no
other way when using a pointer to Interface. But there
are many calls via pointers to AbstractBase or some of
the derived classes. I would like to avoid these
function calls since almost any method of derived
classes do it and the implementation of GetStatus is
really trivial.
Is there any better solution than renaming the method of
Interface to GetStatusByInterface() or something like
that?
Use a forwarding function:
class Base {
public:
void f() { vf(); } // forward to virtual version
protected:
virtual void vf() = 0;
};
class Derived : public Base {
protected:
virtual void vf();
};
class Final : public Derived {
public:
void f() { Final::vf(); } // bypass virtual call
protected:
virtual void vf();
};
void user( Base& b, Derived& d, Final& f )
{
b.f(); // dynamic call to vf
d.f(); // dynamic call to vf
f.f(); // static call to Final::vf
}
Note how it's fine if you pass a Final via a Base&; you'll
get a virtual call. This is one instance where hiding a
(non-virtual) base class function isn't problematic.
Right pattern, but you've inverted the actions:
class Interface
{
public:
int getStatus() const
{
return doGetStatus() ;
}
private:
virtual int doGetStatus() const = 0 ;
} ;
class AbstractBase : public Interface
{
public:
int getStatus() const
{
return currentStatus ;
}
private:
virtual int doGetStatus() const
{
return getStatus() ;
}
private:
int myStatus ;
} ;
It's a fairly standard pattern, I think. (But judging from
other things I've considered "standard patterns", this may
just be wishful thinking. Hasn't anyone read Barton and
Nackman?)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34