This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / numnum, i have a question: Can i define virtual function as inline? Thanks
-mrviceroy(杀人者Daniel是也);
2001-6-22
(#109270@0)
-
no, killer, this is a very tricky one, if you define them as inline, your program will compile, but when you run it, Boom!! the virtual function you have overridden in the child class is not invoked!!
-numnum(numnum);
2001-6-22
(#109277@0)
-
Also, you shouldn't define any virtual functions in the .h header file. because by default, all functions defined in header file are treated "inline"
-numnum(numnum);
2001-6-22
(#109307@0)
-
class boo{ virtual void f()=0; }; class d : public boo { virtual inline f(){cout << "doo::f();" << endl; } }; void main() { d objD; objD.f(); }; // in this case, it compiles and runs. do you think it is inline?
-mrviceroy(杀人者Daniel是也);
2001-6-22
(#109376@0)
-
It should be fine, it seems that you are using a smart compiler. Try to compile this with g++, I had run into problems with VxWorks compiler for PPC603 chip.
-numnum(numnum);
2001-6-22
(#109384@0)
-
For Visual C++, inline is just a suggestion to compiler. If it cannot be inline, compiler just ignores this suggestion. But I still cannot find out whether it is inline.
-mrviceroy(杀人者Daniel是也);
2001-6-22
(#109393@0)
-
killer, I have tested with GNU compiler on Solaris and vxWorks Tornado,
it seems that Solaris has no problem but Tornado crashed. This depends on compilers. Strictly speaking, we shouldn't write inline virtual
functions.
-numnum(numnum);
2001-6-22
(#109641@0)
-
Oh, Sorry, killer, seems the problem is not compiler, it's because that you have everything in the one single file, of course it works. In-line functions are just like Macros, they have the file scope, so I think you will have problem is you implement the virtual function somewhere in a seperate cpp file. Lemme try it out when I have time
-numnum(numnum);
2001-6-22
{207}
(#109398@0)
-
基本正确, but should not be compiler dependent. 这个问题我在五年前花了些时间研究, 后来在面试时还考了几个candidates. 问了三次, 两个印度人都答对, 一个俄国人答错.
-skybook(Skybook);
2001-6-24
{1300}
(#110772@0)
-
"Virtual" means that which function will be called cannot be confirmed untill it runs. "Inline" is like "micro", after compiling, there is no function call at all. So, inline virtual function is meaningless. ....You can pass the compile, but there is no effect when runing. It is just that the compiler ignores such mixed line.
Bye the way, if you write an implement of a funciton(函数体) inside its class defination, the function is automatically an inline function. (you don't need to add 'inline' before it)
-birdincage(birdincage);
2001-6-23
{305}
(#110439@0)