Static in C Static Member Static data members
Static in C++
Static Member • Static data members are: – shared among all instances of the class – exist even if the class does not have an instance in existence • That is, you don’t have one copy of the data member for each instance, you have one variable that created upon startup and is shared.
Static Member • Static members can be accessed without creating object of the class. • it must be located outside of functions and class definitions
Static Function • A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator : : • A static member function can only access static data member.
Inline Function
What is inline Function • An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory.
Why inline Functions • • Eliminate the cost of calls to small functions. Speed up your program. it does not require function calling overhead. it also save overhead of variables push/pop on the stack, while function calling.
Why not inline everything? • Increases the size of code. • After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization. • It may cause compilation overhead as if some body changes code inside inline function than all calling location will also be compiled.
Why not inline everything? • Its not useful for embedded system where large binary size is not preferred at all due to memory size constraints.
Guidelines • Always use inline function when your are sure it will give performance. • Don't inline function with larger code size, one should always inline small code size function to get performance. • In c++, by default member function declared and defined within class get linlined. So no use to specify for such cases.
- Slides: 11