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 | |
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";};
|
|
|
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?
|
|
|
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() : {
|