【 以下文字转载自 JobHunting 讨论区 】 发信人: newdriver (小小的光), 信区: JobHunting 标 题: 问一个C++问题:default parameter and overriding/inheritanc 发信站: BBS 未名空间站 (Tue Feb 22 01:33:31 2011, 美东) class A { private: int n; public: virtual void Fun1(int no=10) { n = no; cout<} }; class B : public A { private: int m; public: virtual void Fun1(int no=20) { m = no; cout<} } int main() { B b; A &a =b; a.Fun1(); return 0; } 这个程序的输出是 B::Func1()10 实在想不明白为什么输出10,而不是20.
c*t
5 楼
都快两个礼拜了,没电话没email的...有消息的请支会一声吧....谢谢咯
s*r
6 楼
后现代主义?
z*e
7 楼
default parameters are statically bound.
A*5
8 楼
顶, 同问。MM哪天面得呀?
r*e
9 楼
g*s
10 楼
so how does the compiler works in details? put a virtual table ptr there, while fill the v_func with 10, as a is nominally class A typed?
【在 z****e 的大作中提到】 : default parameters are statically bound.
A*5
11 楼
刚接到消息的,我是AUDITING方向的。不知道MM问的是哪个方向
s*r
12 楼
后现代主义?
X*r
13 楼
Neither 10 or 20 appears in the vptr at all. The value to pass to the function is determined at the compile time according to the static type of the object, not the dynamic type. 8.3.6 Default arguments 10 A virtual function call uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.
there,
【在 g*********s 的大作中提到】 : so how does the compiler works in details? put a virtual table ptr there, : while fill the v_func with 10, as a is nominally class A typed?
c*t
14 楼
你接到二面电话了? 我TAX啦. 我15号面的,你呢?
d*s
15 楼
凌乱了,赞
P*e
16 楼
vptr只有function address,其他都静态绑定的
【在 X****r 的大作中提到】 : Neither 10 or 20 appears in the vptr at all. The value to : pass to the function is determined at the compile time according : to the static type of the object, not the dynamic type. : 8.3.6 Default arguments : 10 A virtual function call uses the default arguments in the : declaration of the virtual function determined by the static : type of the pointer or reference denoting the object. An : overriding function in a derived class does not acquire default : arguments from the function it overrides. :
【在 g*********s 的大作中提到】 : why not? : a common practice is to put public interface first, following with private : section. in this case how u exclude usage of private?
c*t
26 楼
自己再顶一下... 不要沉下去...
e*s
27 楼
哇,收藏了。
g*s
28 楼
but once u claim public, you need private to switch back. in addition it does't hurt to add one line for better clarity.