由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - interview question: make all class member functions virtual (转载)
相关主题
请教问题一道面试怪题C++. (转载)
基础问题:在header里面define functionclass D:public B;
c++中的inline 函数是做什么的?C++ Template Question
inline functions in C++recursive template?
What happens when recursion functions are declared inline?C++ memcpy declaration use restrict keyword?
C++: friend function关于inline function里的static variable
C++ online Test 又一题 (转载)inline function是否可以递归?
这个function pointer最后的那个int是什么意思?A tech question (转载)
相关话题的讨论汇总
话题: virtual话题: functions话题: child话题: class话题: function
进入Programming版参与讨论
1 (共1页)
f******y
发帖数: 2971
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: hours16 (你好), 信区: JobHunting
标 题: interview question: make all class member functions virtual
发信站: BBS 未名空间站 (Mon Aug 25 21:52:32 2008)
the drawbacks of making all class member functions are virtual,
1. virtual table is bigger and lower the efficiency.
2. any more?
it seems the interviewer expect for more drawback, Thanks
f******y
发帖数: 2971
2
Do we need to redefine the virtual functions, even if they are the same for
both base class and derived class?

【在 f******y 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: hours16 (你好), 信区: JobHunting
: 标 题: interview question: make all class member functions virtual
: 发信站: BBS 未名空间站 (Mon Aug 25 21:52:32 2008)
: the drawbacks of making all class member functions are virtual,
: 1. virtual table is bigger and lower the efficiency.
: 2. any more?
: it seems the interviewer expect for more drawback, Thanks

i******r
发帖数: 323
3
I don't think you need to redefine functions for derived class.

for

【在 f******y 的大作中提到】
: Do we need to redefine the virtual functions, even if they are the same for
: both base class and derived class?

r*******y
发帖数: 290
4
you only make those functions virtual when they are truely needed to be
otherwise it's a mis-design, not a drawback

【在 f******y 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: hours16 (你好), 信区: JobHunting
: 标 题: interview question: make all class member functions virtual
: 发信站: BBS 未名空间站 (Mon Aug 25 21:52:32 2008)
: the drawbacks of making all class member functions are virtual,
: 1. virtual table is bigger and lower the efficiency.
: 2. any more?
: it seems the interviewer expect for more drawback, Thanks

b***y
发帖数: 2799
5
For base classes, it is ok to have more virtual functions because derived
classes may need to customize these functions. For concrete classes, they
may have their own nonvirtual functions that are not supposed to be
overrided.
Also, if you declare a function as virtual, it can't be a inline function.

【在 f******y 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: hours16 (你好), 信区: JobHunting
: 标 题: interview question: make all class member functions virtual
: 发信站: BBS 未名空间站 (Mon Aug 25 21:52:32 2008)
: the drawbacks of making all class member functions are virtual,
: 1. virtual table is bigger and lower the efficiency.
: 2. any more?
: it seems the interviewer expect for more drawback, Thanks

d******n
发帖数: 42
6
it can be inline function but the complier will probably not treat it as
inline function:)
s*******d
发帖数: 59
7
不知道Java的designer会怎么回答这个问题。
g*****g
发帖数: 34805
8
Efficiency is the only benefit of non-virtual function.
By making all non-static functions virtual, Java simplifies
its syntax a lot.

【在 s*******d 的大作中提到】
: 不知道Java的designer会怎么回答这个问题。
F**********r
发帖数: 237
9
The more virtual functions you declare, more places you need to worry about
for complicated overriden behavior...You probably should only declare
virtual when it's really necessary
g*****g
发帖数: 34805
10
On the contrary, it's more complicated in C++ way.
Let's say Child inherits from Parent, both have a do function.
Parent a = new Child();
Child b = new Child();
In C++, a.do() will call Parent.do(), b.do() will call Child.do() if
do() is not virtual.
but both a and b are an instance of Child in runtime. This can
be confusing, you have to check declaration of Parent before you know
whether it's virtual and which function is called.
In java, it's always calling Child.do().

about

【在 F**********r 的大作中提到】
: The more virtual functions you declare, more places you need to worry about
: for complicated overriden behavior...You probably should only declare
: virtual when it's really necessary

1 (共1页)
进入Programming版参与讨论
相关主题
A tech question (转载)What happens when recursion functions are declared inline?
inline C function in VC++ 2005 expressC++: friend function
有什么办法可以查每行代码用的时间?C++ online Test 又一题 (转载)
[合集] Inline member function in C++这个function pointer最后的那个int是什么意思?
请教问题一道面试怪题C++. (转载)
基础问题:在header里面define functionclass D:public B;
c++中的inline 函数是做什么的?C++ Template Question
inline functions in C++recursive template?
相关话题的讨论汇总
话题: virtual话题: functions话题: child话题: class话题: function