virtual function
-
Inside <>, it wrote "It is stll a bad design choice to declare all functions virutal and to depend on the compiler to optimize away unnecessary virtual invocations." I can't understand it very well. class Abstract_base { public: virtual ~Abstract_base()=0; virtual void interface1() const = 0; virtual const char* mumble() const {return _mumble;) protected: char *_mumble; }; As the source above. "virtual const char* mumble() const {return _mumble;)" If you define a member function to be virtual, you insert a vptr in your class object. but as the function's defination has been claimed within the head file, it must be looked as an linline function. Is it right? Tomorrow is another day!
-
Inside <>, it wrote "It is stll a bad design choice to declare all functions virutal and to depend on the compiler to optimize away unnecessary virtual invocations." I can't understand it very well. class Abstract_base { public: virtual ~Abstract_base()=0; virtual void interface1() const = 0; virtual const char* mumble() const {return _mumble;) protected: char *_mumble; }; As the source above. "virtual const char* mumble() const {return _mumble;)" If you define a member function to be virtual, you insert a vptr in your class object. but as the function's defination has been claimed within the head file, it must be looked as an linline function. Is it right? Tomorrow is another day!
I think what the author meant was that the compiler does not generate a vtable entry that is never needed. If you declare a function as virtual in a base class, and that function is always called in the scope of a known class, the compiler might as well skip the vtable entry for that function. Or if you declare a virtual fuction in a class with no sub- or superclasses, there's no point in using a vtable at all. As for the inline declaration of a function, it only tells the compiler that you would prefer to have the code generated inline. If the function is virtual, you will still get a pointer in the vtable to that function (unless optimized away by the compiler). Might add that I'm not an expert on the inner logic of an actual compiler, so the case may be a bit more complicated than this :-) -- modified at 12:33 Thursday 25th October, 2007