hello everybody. i have some simple questions. what are inline and virtual for? i did some search, but nothing really clarifying. i just understand that inline copies the function to where should be only a call for it, but what is it's advantage?
also, what are "symbols"? Wikipedia says
A symbol in computer programming is a primitive datatype whose instances have a unique human-readable form.
so, symbols are the comment lines? haha i have no idea besides that...
Inline is an instruction for the compiler to place the full body of the function where ever it is called. Basically eliminating some of the overhead when a function is called.
Virtual is to be overridden by the derived classes.
thanks!
so, if inline just elimines some overhead, why not to use it in every function I call? is it because the final application would become much bigger?
and virtual, then, should only be used in parent classes, just to indicate that it can be overriden?
"inline" cannot be used for every function for two main reasons:
1) "inline" is a suggestion to the compiler that it should in-line the function. Though, the compiler can completely disregard the suggestion. Based on the set optimisation flags, the compiler can pick which functions to in-line based on the optimisation scheme it has in-place.
2) While in-lining increases performance, it can also reduce it. If a function is in-lined but called infrequently, the benefits of in-lining won't affect overall program performance all that much; if a function is in-lined but called excessively, you'll reduce the effectiveness of the CPU cache, thereby reducing locality of reference.
In regards to "virtual", the compiler can implicitly mark a member-function as "virtual" if a derived class overrides a member-function of a base class. Though, the general rule-of-thumb is to only declare functions "virtual" when either the member function must be overridden (pure-virtual functions) or when a function should be overridden by the derived class if the default implementation of the function is not suitable for the derived class.
"but why would i create functions that need to be overriden (pure virtual)? why to create somethig that won't be used?"
Pure virtual classes are used to implement abstract classes, while "virtual" (non-pure) are used to implement polymorphic classes. Sometimes, we need a derived class to implement a function. Take an "interface" design, for example. Interfaces consist of pure-virtual functions, which, together, create a common interface between all classes that derive from the interface. Here's a simple example:
Here, "ICommon::GetGender( )" cannot have a body because we cannot assume the gender of the derived class, so we have force the derived class to return its gender via "ICommon::GetGender( )".
Also, member-functions of an interface must be overridden or else the derived class, too, becomes an abstract class.
inline means a function can be multiply-defined, that is, you can put the function definition in a header file, #include it in several .cpp files, and the resulting program will compile and link successfully. (note that all functions defined inside class definitions are automatically inline, since class definitions usually go into header files).