n*******r 发帖数: 22 | 1 【 以下文字转载自 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<<"A::Fun1"<
}
};
class B : public A
{
private:
int m;
public:
virtual void Fun1(int no=20)
{
m = no;
cout<<"B::Fun1()"<
}
}
int main()
{
B b;
A &a =b;
a.Fun1();
return 0;
}
这个程序的输出是 B::Func1()10
实在想不明白为什么输出10,而不是20. |
z****e 发帖数: 2024 | 2 default parameters are statically bound. |
g*********s 发帖数: 1782 | 3 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.
|
X****r 发帖数: 3557 | 4 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?
|
P********e 发帖数: 2610 | 5 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. :
|
c**b 发帖数: 2999 | 6 谁写的蹩脚程序,class 里面还要加private?
【在 n*******r 的大作中提到】 : 【 以下文字转载自 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)
|
g*********s 发帖数: 1782 | 7 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**b 的大作中提到】 : 谁写的蹩脚程序,class 里面还要加private?
|
c**b 发帖数: 2999 | 8 class里面的都默认是private的,根本不用加private.
【在 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?
|
g*********s 发帖数: 1782 | 9 but once u claim public, you need private to switch back.
in addition it does't hurt to add one line for better clarity.
【在 c**b 的大作中提到】 : class里面的都默认是private的,根本不用加private.
|