由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个C++问题:default parameter and overriding/inheritanc (转载)
相关主题
问个虚函数的作用Two questions on virtual destructor
C++小插曲ajax小问题
Default function template argumentsQuestion about data member offset
师傅们都出来看看吧,我也问个C++返回值问题。为什么derived object没有vptr?
How to check the virtual function table size?我有个很傻的问题,关于function call via pointer
问题:vptr/vtable for virtual function & vptr/vtable forstatic如何作为函数?
A C++ compiler related interview questiontwo c++ interview questions! (转载)
c++里的函数可不可以是virtual+staticone more interview question
相关话题的讨论汇总
话题: fun1话题: default话题: overriding话题: int话题: c++
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
one more interview questionHow to check the virtual function table size?
Question: How to store initialize arguments?问题:vptr/vtable for virtual function & vptr/vtable for
一个c++问题 (转载)A C++ compiler related interview question
copy constructor问题。c++里的函数可不可以是virtual+static
问个虚函数的作用Two questions on virtual destructor
C++小插曲ajax小问题
Default function template argumentsQuestion about data member offset
师傅们都出来看看吧,我也问个C++返回值问题。为什么derived object没有vptr?
相关话题的讨论汇总
话题: fun1话题: default话题: overriding话题: int话题: c++