由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 非虚函数里调用虚函数无效?
相关主题
问一个简单C++问题问个C++ virtual function的问题 (转载)
C++ 弱问一个C++ Q98: Call member function in virtual function (转载)
请教 C++的一个困惑 (operator delete)One c++ non-type template question
问个虚函数的作用 请问c++为什么会编译失败?
[合集] C++ virtual function的一个问题。。哪位大牛简单说说compiler里的bootstrap是干啥用的?
虚函数access权限改变问题question for C++ constant
C++问题C++ 的 问题
private destructorA C++ compiler related interview question
相关话题的讨论汇总
话题: fb话题: void话题: fa话题: export话题: main
进入Programming版参与讨论
1 (共1页)
d******i
发帖数: 7160
1
class B
{ void fa(){fb();}
virtual void fb()
}
class D: public B
{
void fb(){cout<<"what i want"};
}
main()
{
B* d=new D;
d->fb();
d->fa();
}
结果d->fb()能看到"what i want",
可是d->fa()看不到,除非把fa()也virtual了。
何解?请指教!
d******i
发帖数: 7160
2
急啊,在线等。
有人明白吗?
t****t
发帖数: 6806
3
把code贴全了先.

【在 d******i 的大作中提到】
: class B
: { void fa(){fb();}
: virtual void fb()
: }
: class D: public B
: {
: void fb(){cout<<"what i want"};
: }
: main()
: {

j*a
发帖数: 14423
4
加一句debug你就明白了呗

【在 d******i 的大作中提到】
: class B
: { void fa(){fb();}
: virtual void fb()
: }
: class D: public B
: {
: void fb(){cout<<"what i want"};
: }
: main()
: {

S**I
发帖数: 15689
5
make your code correct first

【在 d******i 的大作中提到】
: class B
: { void fa(){fb();}
: virtual void fb()
: }
: class D: public B
: {
: void fb(){cout<<"what i want"};
: }
: main()
: {

c*********e
发帖数: 16335
6
late binding

【在 d******i 的大作中提到】
: class B
: { void fa(){fb();}
: virtual void fb()
: }
: class D: public B
: {
: void fb(){cout<<"what i want"};
: }
: main()
: {

f****p
发帖数: 18483
7
你那个B::fb()必须得提供implementation。就不说什么include和那些分号了,你这个
code根本就build不了。
G*****7
发帖数: 1759
8
#include
using namespace std;
struct B
{
void fa() {fb();};
virtual void fb() { cout << __FUNCTION__; };
};
struct D: public B
{
void fb(){cout<<"what i want\n";};
} ;
void main()
{
B* d=new D;
d->fb();
d->fa();
}
Console output:
what i want
what i want

【在 d******i 的大作中提到】
: class B
: { void fa(){fb();}
: virtual void fb()
: }
: class D: public B
: {
: void fb(){cout<<"what i want"};
: }
: main()
: {

j*****g
发帖数: 16
9
即使在member function里面用virtual function也是 late binding.d->fa(),d是指
向D的object的pointer,在fa()里面的时候*this是指向D的object.
c*******y
发帖数: 1630
10
c++ 还接受main return void?

【在 G*****7 的大作中提到】
: #include
: using namespace std;
: struct B
: {
: void fa() {fb();};
: virtual void fb() { cout << __FUNCTION__; };
: };
: struct D: public B
: {
: void fb(){cout<<"what i want\n";};

相关主题
虚函数access权限改变问题问个C++ virtual function的问题 (转载)
C++问题C++ Q98: Call member function in virtual function (转载)
private destructorOne c++ non-type template question
进入Programming版参与讨论
N8
发帖数: 110
11
yes

【在 c*******y 的大作中提到】
: c++ 还接受main return void?
N8
发帖数: 110
12
and for "int main(){
}"
you can omit "return" too

【在 N8 的大作中提到】
: yes
G*****7
发帖数: 1759
13
g++ doesnt. cl.exe (not clang) does.

【在 c*******y 的大作中提到】
: c++ 还接受main return void?
t****t
发帖数: 6806
14
it's allowed in freestanding environment. freestanding basically is like "
limited support"...

【在 N8 的大作中提到】
: yes
t****t
发帖数: 6806
15
omit return in main() is specifically allowed in standard. "void main()" is
like 山寨版实现. these two are totally different.

【在 N8 的大作中提到】
: and for "int main(){
: }"
: you can omit "return" too

G*****7
发帖数: 1759
16
cl.exe is non-standard in a lot of places. this is one of them.

is

【在 t****t 的大作中提到】
: omit return in main() is specifically allowed in standard. "void main()" is
: like 山寨版实现. these two are totally different.

c*******y
发帖数: 1630
17
被你打败了。

【在 N8 的大作中提到】
: and for "int main(){
: }"
: you can omit "return" too

t****t
发帖数: 6806
18
actually, template implementation separately in cpp is also allowed in
standard. this is also different from "void main()" crap.
cl.exe is non-standard in a lot of places, that part i agree. but you made
two bad examples.

【在 G*****7 的大作中提到】
: cl.exe is non-standard in a lot of places. this is one of them.
:
: is

G*****7
发帖数: 1759
19
my prev post was wrong. i was thinking about a case of template implemented
in cpp without explicit instantiation or export. that turns out to be
problematic. an instantiation was hidden somewhere in the function
implementations.
what does the latest standard say about implemenations in cpp? can we do it
without inclusion into .h, explicit inst or export?

【在 t****t 的大作中提到】
: actually, template implementation separately in cpp is also allowed in
: standard. this is also different from "void main()" crap.
: cl.exe is non-standard in a lot of places, that part i agree. but you made
: two bad examples.

N8
发帖数: 110
20

implemented
it
~~~~~~~~~~~~~~~ If my memory is right, it is no for export. but I may be
wrong

【在 G*****7 的大作中提到】
: my prev post was wrong. i was thinking about a case of template implemented
: in cpp without explicit instantiation or export. that turns out to be
: problematic. an instantiation was hidden somewhere in the function
: implementations.
: what does the latest standard say about implemenations in cpp? can we do it
: without inclusion into .h, explicit inst or export?

相关主题
请问c++为什么会编译失败?C++ 的 问题
哪位大牛简单说说compiler里的bootstrap是干啥用的?A C++ compiler related interview question
question for C++ constantAn interesting C++ compile error
进入Programming版参与讨论
t****t
发帖数: 6806
21
i think you are talking about exported template. exported template is
equivalent to implicitly include the definition in every translation unit. "
export" keyword exists in c++98, which means all conforming compilers should
support it. that's the theory.
however in reality, very few compilers support it. specifically, neither vc+
+ nor g++ support the keyword. considering the reality, c++11 removed export
keyword completely, but reserved the keyword for future use.
so no more "exported" template in the foreseeable future.

implemented
it

【在 G*****7 的大作中提到】
: my prev post was wrong. i was thinking about a case of template implemented
: in cpp without explicit instantiation or export. that turns out to be
: problematic. an instantiation was hidden somewhere in the function
: implementations.
: what does the latest standard say about implemenations in cpp? can we do it
: without inclusion into .h, explicit inst or export?

N8
发帖数: 110
22
as I aware, there is only one compiler supports it.
maybe there are one or two more

"
should
vc+
export

【在 t****t 的大作中提到】
: i think you are talking about exported template. exported template is
: equivalent to implicitly include the definition in every translation unit. "
: export" keyword exists in c++98, which means all conforming compilers should
: support it. that's the theory.
: however in reality, very few compilers support it. specifically, neither vc+
: + nor g++ support the keyword. considering the reality, c++11 removed export
: keyword completely, but reserved the keyword for future use.
: so no more "exported" template in the foreseeable future.
:
: implemented

a**********e
发帖数: 157
23
可能:
fa不是virtual,所以 当call d->fa() 的时候,'d'' in practice 相当
于一个真正的B*.当 它寻找 fb的时候,因为这种search不cross
namespace,所以就直接call B::fb()了。

【在 d******i 的大作中提到】
: class B
: { void fa(){fb();}
: virtual void fb()
: }
: class D: public B
: {
: void fb(){cout<<"what i want"};
: }
: main()
: {

1 (共1页)
进入Programming版参与讨论
相关主题
A C++ compiler related interview question[合集] C++ virtual function的一个问题。。
An interesting C++ compile error虚函数access权限改变问题
C++怎么不打印小数结尾的0C++问题
C++ template problemprivate destructor
问一个简单C++问题问个C++ virtual function的问题 (转载)
C++ 弱问一个C++ Q98: Call member function in virtual function (转载)
请教 C++的一个困惑 (operator delete)One c++ non-type template question
问个虚函数的作用 请问c++为什么会编译失败?
相关话题的讨论汇总
话题: fb话题: void话题: fa话题: export话题: main