由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A C++ compiler related interview question
相关主题
C++小插曲再问C++问题。
An interesting C++ compile errorinline到底能省多少时间?
我有个很傻的问题,关于function call via pointerA tech question (转载)
问个虚函数的作用谁来解释一下这个是compiler问题吗?
c++ dynamic cast关于Makefile的一个问题
C array关于C++ STL编译的疑问
c++ define 一问question for C++ constant
码工试题 (转载)a question about CAST
相关话题的讨论汇总
话题: virtual话题: bar话题: base话题: derived话题: c++
进入Programming版参与讨论
1 (共1页)
s*******e
发帖数: 27
1
I was asked an iterview question and I was kind of confused at the time.
Please tell me if the following code compiles and why?
Thx.
class base
{
public:
virtual void foo(){}; //virtual
};
class derived : public base
{
public:
void foo(){}; //virtual
void bar(){}; //not a vritual function
};
base* p = new derived;
p->bar(); //will this compile and why before you run it.
z****e
发帖数: 2024
2
no, it will not compile, because bar is not virtual and vptr can not lookup the vtable and find it. so it should fail. bar is not even a member of base.
actually, even bar() is virtual but added by Derived, through base class pointer to call it, is a compile time error. you can downcast using
dynamic_cast(p)->bar() to finish the call.
s*******e
发帖数: 27
3
Thanks a lot.

lookup the vtable and find it. so it should fail. bar is not even a member
of base.
pointer to call it, is a compile time error. you can downcast using

【在 z****e 的大作中提到】
: no, it will not compile, because bar is not virtual and vptr can not lookup the vtable and find it. so it should fail. bar is not even a member of base.
: actually, even bar() is virtual but added by Derived, through base class pointer to call it, is a compile time error. you can downcast using
: dynamic_cast(p)->bar() to finish the call.

1 (共1页)
进入Programming版参与讨论
相关主题
a question about CASTc++ dynamic cast
c++标准函数传递一问C array
C++ 的 问题c++ define 一问
有谁知道怎么把matlab和VC联接着一起用? (转载)码工试题 (转载)
C++小插曲再问C++问题。
An interesting C++ compile errorinline到底能省多少时间?
我有个很傻的问题,关于function call via pointerA tech question (转载)
问个虚函数的作用谁来解释一下这个是compiler问题吗?
相关话题的讨论汇总
话题: virtual话题: bar话题: base话题: derived话题: c++