Inline Functions and Inline Member Functions in C++ with Examples
An Inline function is a function declared using the inline keyword. It allows a function to be defined in a way that permits multiple identical definitions across translation units, subject to the One Definition Rule (ODR). It also traditionally serves as a request to the compiler to consider replacing a function call with the function's code. The compiler is not required to perform inline substitution. It decides whether to inline a function based on optimization and other factors.
Inline Function वह function होता है जिसे inline keyword की सहायता से declare किया जाता है। यह function को इस प्रकार define करने की सुविधा देता है कि ODR के नियमों के अंतर्गत इसकी समान definitions multiple translation units में हो सकती हैं। Traditionally, यह compiler को function call की जगह function का code रखने का सुझाव भी देता है। Compiler के लिए inline substitution करना अनिवार्य नहीं होता। वह optimization और अन्य factors के आधार पर निर्णय लेता है।
1. Defining an Inline Function Using the inline Keyword
In C++, the inline keyword is used before the function's return type to declare an inline function.
C++ में inline function declare करने के लिए function के return type से पहले inline keyword का उपयोग किया जाता है।
Syntax
inline return_type functionName(parameters){// Function Body}
Example: Inline Function in C++
#include <iostream>using namespace std;inline int square(int n){return n * n;}int main(){int num = 5;cout << "Number: " << num << endl;cout << "Square: " << square(num);return 0;}
Output:
Number: 5Square: 25
Explanation:
inlineis the keyword used to declare an inline function.square()is an inline function.It accepts an integer argument and returns its square.
The function is called using
square(num).
व्याख्या:
inlineएक keyword है जिसका उपयोग inline function declare करने के लिए किया जाता है।square()एक inline function है।यह एक integer argument accept करता है और उसका square return करता है।
Function को
square(num)की सहायता से call किया गया है।
2. Advantages of Inline Functions in C++
Inline functions offer several benefits when used appropriately, especially for small functions.
Inline Functions का सही तरीके से उपयोग करने पर कई फायदे मिलते हैं, विशेष रूप से छोटे functions के लिए।
Reduced Function Call Overhead: When the compiler performs inline substitution, the overhead of a normal function call may be avoided.
Function Call Overhead में कमी: जब compiler inline substitution करता है, तो सामान्य function call का overhead कम हो सकता है।
Suitable for Small Functions: Inline functions are commonly useful for short functions that perform simple operations.
छोटे Functions के लिए उपयोगी: Inline functions छोटे और simple operations करने वाले functions के लिए उपयोगी हो सकते हैं।
Code Organization: Small functions can be defined clearly while keeping the program modular.
Code Organization: छोटे functions को define करके program को modular और व्यवस्थित बनाया जा सकता है।
Type Checking: Unlike macros, inline functions follow normal C++ function type-checking rules.
Type Checking: Macros के विपरीत, inline functions सामान्य C++ function type-checking rules follow करते हैं।
3. Inline Member Functions in C++
An Inline member function is a member function of a class that is inline. A member function defined inside the class definition is implicitly inline, subject to the rules of C++. Inline member functions are useful for short operations involving class data members.
Inline Member Function वह member function होता है जो किसी class का हिस्सा होता है और inline होता है। Class definition के अंदर define किया गया member function C++ के नियमों के अनुसार implicitly inline होता है। Inline Member Functions का उपयोग class के data members पर छोटे operations perform करने के लिए किया जा सकता है।
4. Defining Inline Member Functions Inside a Class
When a member function is defined inside the class, its complete function body is written within the class definition. Such a function is implicitly inline.
जब किसी member function को class के अंदर define किया जाता है, तो उसका पूरा function body class definition के अंदर लिखा जाता है। ऐसा function implicitly inline होता है।
Example: Inline Member Function Defined Inside a Class
#include <iostream>using namespace std;class Rectangle{private:int length;int breadth;public:void setData(int l, int b){length = l;breadth = b;}int area(){return length * breadth;}};int main(){Rectangle r1;r1.setData(10, 5);cout << "Area of Rectangle: " << r1.area();return 0;}
Output:
Area of Rectangle: 50Explanation:
Rectangleis a class.lengthandbreadthare private data members.setData()andarea()are member functions.Both member functions are defined inside the class and are implicitly inline.
The object
r1is used to call these functions.
व्याख्या:
Rectangleएक class है।lengthऔरbreadthprivate data members हैं।setData()औरarea()member functions हैं।दोनों member functions class के अंदर define किए गए हैं, इसलिए ये implicitly inline हैं।
इन functions को call करने के लिए
r1object का उपयोग किया गया है।
5. Defining Inline Member Functions Outside a Class
A member function can also be defined outside the class using the scope resolution operator (::). If we want to explicitly declare such a function as inline, we use the inline keyword in its definition.
Member function को class के बाहर Scope Resolution Operator (::) की सहायता से भी define किया जा सकता है। यदि ऐसे function को explicitly inline declare करना हो, तो उसकी definition में inline keyword का उपयोग किया जाता है।
Syntax
class ClassName{public:return_type functionName(parameters);};inline return_type ClassName::functionName(parameters){// Function Body}
Example: Inline Member Function Defined Outside a Class
#include <iostream>using namespace std;class Rectangle{private:int length;int breadth;public:void setData(int l, int b);int area();};inline void Rectangle::setData(int l, int b){length = l;breadth = b;}inline int Rectangle::area(){return length * breadth;}int main(){Rectangle r1;r1.setData(8, 4);cout << "Area of Rectangle: " << r1.area();return 0;}
Output:
Area of Rectangle: 32Explanation:
The
Rectangleclass declares two member functions:setData()andarea().Their definitions are written outside the class.
The scope resolution operator (
::) specifies that these functions belong to theRectangleclass.The
inlinekeyword is used explicitly in their definitions.
व्याख्या:
Rectangleclass मेंsetData()औरarea()नाम के दो member functions declare किए गए हैं।इनकी definitions class के बाहर लिखी गई हैं।
Scope Resolution Operator (
::) बताता है कि ये functionsRectangleclass के members हैं।इनकी definitions में explicitly
inlinekeyword का उपयोग किया गया है।
Note: For a function declared in a header file and defined outside the class, the inline definition should generally be visible in every translation unit that uses it. This is commonly achieved by placing the definition in the header file.
ध्यान दें: यदि किसी function को header file में declare करके class के बाहर define किया जाता है, तो उसकी inline definition सामान्यतः प्रत्येक उस translation unit में उपलब्ध होनी चाहिए जो उसका उपयोग करती है। इसके लिए definition को header file में रखा जाता है।
6. Difference Between Inline Functions and Inline Member Functions
An inline function can be a standalone function, whereas an inline member function belongs to a class.
Inline Function एक standalone function हो सकता है, जबकि Inline Member Function किसी class का member होता है।
| Basis | Inline Function | Inline Member Function |
|---|---|---|
| Meaning | A standalone function declared inline | A class member function that is inline |
| Declaration | Declared outside a class | Declared inside a class or as a class member |
| Access | Cannot directly access private class members without appropriate access | Can access the class's private and protected members |
| Definition | Can be defined outside a class | Can be defined inside or outside a class |
| Example | inline int square(int n) | int area() defined inside a class |
Important Note About Inline Functions
The inline keyword does not guarantee that the compiler will replace every function call with the function body. Modern C++ compilers make their own optimization decisions. Also, a function defined inside a class is implicitly inline even if the inline keyword is not written explicitly.
inline keyword इस बात की guarantee नहीं देता कि compiler प्रत्येक function call को function body से replace करेगा। Modern C++ compilers अपने optimization decisions स्वयं लेते हैं। इसके अलावा, class के अंदर define किया गया function inline keyword explicitly लिखे बिना भी implicitly inline होता है।
Inline functions are useful for organizing small functions and may reduce function call overhead when the compiler performs inline substitution. Inline member functions are class member functions that follow the same inline rules. In C++, member functions defined inside a class are implicitly inline, while functions defined outside the class can be explicitly declared inline using the inline keyword.
Inline Functions छोटे functions को व्यवस्थित करने के लिए उपयोगी होते हैं और compiler द्वारा inline substitution किए जाने पर function call overhead कम कर सकते हैं। Inline Member Functions वे class member functions होते हैं जो inline के समान नियमों का पालन करते हैं। C++ में class के अंदर define किए गए member functions implicitly inline होते हैं, जबकि class के बाहर define किए गए functions को inline keyword की सहायता से explicitly inline declare किया जा सकता है।
Comments
Post a Comment