Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Classes

Friend Function

Friend functions in C++ are a special type of functions that are granted access to private and protected members of a class, even though they are not members of that class.

Key points about friend functions:

  1. Declaration: Friend functions are declared inside the class definition using the friend keyword, but they are not member functions of the class.

  2. Access: They can access private and protected members of the class without being a member of that class.

  3. Scope: They are not in the scope of the class, and they don't have a this pointer.

  4. Declaration vs. Definition: The function can be declared as a friend in the class definition, but the actual function definition is outside the class.

  5. Cannot be inherited: Friendship is not inherited, meaning if a base class has a friend function, it doesn't automatically become a friend of the derived class.

Here's a simple example to illustrate friend functions:

#include <iostream>

class Box {
private:
    int width, height, depth;

public:
    Box(int w, int h, int d) : width(w), height(h), depth(d) {}

    // Declaration of friend function
    friend int getBoxVolume(const Box& b);

    // Friend function can be a member of another class
    friend class BoxPrinter;
};

// Definition of friend function
int getBoxVolume(const Box& b) {
    // Can access private members of Box
    return b.width * b.height * b.depth;
}

class BoxPrinter {
public:
    void printBoxDimensions(const Box& b) {
        // Can access private members of Box
        std::cout << "Width: " << b.width 
                  << ", Height: " << b.height 
                  << ", Depth: " << b.depth << std::endl;
    }
};

int main() {
    Box myBox(3, 4, 5);
    std::cout << "Volume: " << getBoxVolume(myBox) << std::endl;

    BoxPrinter printer;
    printer.printBoxDimensions(myBox);

    return 0;
}

In this example:

  1. getBoxVolume is a friend function of the Box class. It can access private members of Box.

  2. BoxPrinter is a friend class of Box. All its member functions can access private members of Box.

  3. The getBoxVolume function and BoxPrinter::printBoxDimensions method can access width, height, and depth directly, even though they're private.

Use cases for friend functions:

  1. When you need a function to have access to private members of a class, but you don't want it to be a member function.

  2. For operator overloading, especially when the left-hand operand is not an object of the class (like stream insertion operators).

  3. To allow a function to work intimately with two different classes.

PreviousClassesNextCopy Constructor

Last updated 9 months ago

Run it .

🏢
here