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
|