请教一个同名虚成员函数、成员函数重载的问题。
环境:gcc 4.8.1
在arm上写基于framebuffer的图形程序,QT之类的跑得太慢,只有自己写些简单的绘图类。遇到这样一个问题,比如画矩形框,由于颜色位数(4,8,16位色)不同,就将最基础的功能写成虚函数,在派生类里实现。例如:
基类:
virtual void drawRect(int x,int y,int width,int height){}
inline void drawRect(const Rect &rect){
drawRect(rect.x, rect.y, rect.width, rect.height);
}
inline void drawRect(const Rect *rect,int count){
for(;count;count--) drawRect(*rect++);
}
不同派生类:
void drawRect(int x,int y,int width,int height)
{
///具体实现
}
实际测试发现,基类里的drawRect(const Rect &rect)没有被派生类继承。最后只能改成这样:
基类:
virtual void _drawRect(int x,int y,int width,int height){}
inline void drawRect(int x,int y,int width,int height){
_drawRect(x, y, width, height);
}
inline void drawRect(const Rect &rect){
_drawRect(rect.x, rect.y, rect.width, rect.height);
}
inline void drawRect(const Rect *rect,int count){
for(;count;count--) drawRect(*rect++);
}
不同派生类:
void _drawRect(int x,int y,int width,int height)
{
///具体实现
}
请高人指点一下为什么前一种方案不行。有没有更好的解决办法?