Tutorial on Inline Implementation in C++
By: Jagan in C++ Tutorials on 2007-09-09
Just as you can ask the compiler to make a regular function inline, you can make class methods inline. The keyword inline appears before the return value. The inline implementation of the GetWeight() function, for example, looks like this:
inline int Cat::GetWeight() { return itsWeight; // return the Weight data member }
You can also put the definition of a function into the declaration of the class, which automatically makes that function inline. For example,
class Cat { public: int GetWeight() { return itsWeight; } // inline void SetWeight(int aWeight); };
Note the syntax of the GetWeight() definition. The body of the inline function begins im-mediately after the declaration of the class method; there is no semicolon after the paren-theses. Like any function, the definition begins with an opening brace and ends with a closing brace. As usual, whitespace doesn't matter; you could have written the declaration as
class Cat { public: int GetWeight() { return itsWeight; } // inline void SetWeight(int aWeight); };
The following two programs re-create the Cat class, but they put the declaration in CAT.HPP and the implementation of the functions in CAT.CPP. The second program below also changes the accessor functions and the Meow() function to inline.
Cat class declaration in CAT.HPP
1: #include <iostream.h> 2: class Cat 3: { 4: public: 5: Cat (int initialAge); 6: ~Cat(); 7: int GetAge() { return itsAge;} // inline! 8: void SetAge (int age) { itsAge = age;} // inline! 9: void Meow() { cout << "Meow.\n";} // inline! 10: private: 11: int itsAge; 12: };
Cat implementation in CAT.CPP.
1: // Demonstrates inline functions 2: // and inclusion of header files 3: 4: #include "cat.hpp" // be sure to include the header files! 5: 6: 7: Cat::Cat(int initialAge) //constructor 8: { 9: itsAge = initialAge; 10: } 11: 12: Cat::~Cat() //destructor, takes no action 13: { 14: } 15: 16: // Create a cat, set its age, have it 17: // meow, tell us its age, then meow again. 18: int main() 19: { 20: Cat Frisky(5); 21: Frisky.Meow(); 22: cout << "Frisky is a cat who is " ; 23: cout << Frisky.GetAge() << " years old.\n"; 24: Frisky.Meow(); 25: Frisky.SetAge(7); 26: cout << "Now Frisky is " ; 27: cout << Frisky.GetAge() << " years old.\n"; 28: return 0; 29: } Output: Meow. Frisky is a cat who is 5 years old. Meow. Now Frisky is 7 years old.
Analysis: Three of the methods are
written inline in the declaration file and the declaration has been separated
into CAT.HPP.
GetAge() is declared in line 7, and its inline implementation is
provided. Lines 8 and 9 provide more inline functions, but the functionality of
these functions is unchanged from the previous "outline"
implementations.
Line 4 shows #include "cat.hpp", which brings in the listings from CAT.HPP. By including cat.hpp, you have told the precompiler to read cat.hpp into the file as if it had been typed there, starting on line 5.
This technique allows you to put your declarations into a different file from your implementation, yet have that declaration available when the compiler needs it. This is a very common technique in C++ programming. Typically, class declarations are in an .HPP file that is then #included into the associated CPP file.
Lines 18-29 making these functions inline doesn't change their performance.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments