W
want.to.be.professer
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 )?
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 )?