Friend Classes and Functions

Inline Friend Functions

Can a friend function be declared inline, and what are the implications?

Abstract art representing computer programming

Yes, a friend function can be declared inline. Declaring a function inline suggests to the compiler to insert the function's code directly at the point of call, which can improve performance by avoiding function call overhead.

Declaring an Inline Friend Function

An inline friend function is defined inside the class declaration:

#include <iostream>

class MyClass {
  friend void showValue(MyClass &obj) {           
    std::cout << "Value: " << obj.value << "\n";  
  }

 private:
  int value{42};
};

int main() {
  MyClass obj;
  showValue(obj);
}
Value: 42

In this example, showValue() is an inline friend function defined within the class. It has access to the private member value.

Benefits

  • Performance: Inlining can reduce the overhead associated with function calls, especially for small, frequently called functions.
  • Readability: Defining the function within the class can make the code easier to read by keeping related code together.

Implications

  • Code Size: Excessive inlining can increase the binary size of the program because the function's code is duplicated at each call site.
  • Compiler Discretion: The inline keyword is a suggestion to the compiler. The compiler can choose to ignore it if inlining is not deemed beneficial.
  • Debugging: Inlined code can make debugging more difficult since the function's body is spread across multiple call sites.

Example

Here’s an example with multiple inline friend functions:

#include <iostream>

class Rectangle {
  friend int calculateArea(Rectangle &rect) { 
    return rect.width * rect.height; 
  }
  friend void displayDimensions(Rectangle &rect) { 
    std::cout << "\nWidth: " << rect.width
      << ", Height: " << rect.height; 
  }

public:
  Rectangle(int w, int h) : width{w}, height{h} {}

private:
  int width, height;
};

int main() {
  Rectangle rect(10, 20);
  std::cout << "Area: " << calculateArea(rect);
  displayDimensions(rect);
}
Area: 200
Width: 10, Height: 20

Here, calculateArea() and displayDimensions() are both inline friend functions. They provide efficient access to the Rectangle's private members.

Conclusion

Inline friend functions can optimize performance by reducing function call overhead and can improve readability by keeping related code together. However, use inlining judiciously to avoid potential increases in code size and complications in debugging.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved