×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / numnum, i have a question: Can i define virtual function as inline? Thanks
    • 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!!
      • 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"
        • 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?
          • 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.
            • 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.
              • 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.
            • 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
            • 基本正确, but should not be compiler dependent. 这个问题我在五年前花了些时间研究, 后来在面试时还考了几个candidates. 问了三次, 两个印度人都答对, 一个俄国人答错.
              本文发表在 rolia.net 枫下论坛Sorry I have to write the rest in English. The ANCI C++ working paper indicates that "inline" is not required, but should be respected.

              If a compiler is implemented correctly, it should handle inline virtual as follows: the function can be inlined if the compiler knows the actual object. However if the method invocation is via a pointer or a reference, then the inline part should be ignored by the compiler since which method to be invoked will be determined at runtime. Some compilers may decide to inline a method invoked via a pointer to an object whose dynamic type is known at compile time, which is fine.

              Also some compilers decide where to put the vtbl based on that translation unit where you defined the first (or last or whatever) non-pure,non-inline virtual function, so if you happen to use such a compiler and have declared *all* your virtual function inline, this could be a problem.

              Compilers are occasionally implemented incorrectly. I remember on the average one out of six compilers handle smart pointer incorrectly by deleting an extra reference counting due to promotion. As a result three years later (or even longer, or maybe never) the application crashes due to the client code trys to access a non exist object. These kinds of bugs are extreamly to find.更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • "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)