Home  >  Article  >  Backend Development  >  What impact do friend functions have on class inheritance?

What impact do friend functions have on class inheritance?

PHPz
PHPzOriginal
2024-04-17 08:33:02805browse

Inheritance of friend functions When a subclass inherits a class with friend functions: The subclass cannot inherit the friend function. Friend functions of the parent class can access private members of the child class. Friend functions of a subclass cannot access private members of the parent class.

What impact do friend functions have on class inheritance?

The influence of friend functions on class inheritance

Preface

Friends A metafunction is a special C function that can access private members of a class outside the scope of the class. When it comes to class inheritance, understanding the behavior of friend functions is crucial.

Friend Functions and Inheritance

When a subclass inherits a class that has friend functions, the following rules apply:

  • Subclasses cannot inherit friend functions: Friend functions are associated with a specific class. Subclasses cannot inherit friend functions of parent classes.
  • Friend functions of the parent class can access the private members of the subclass: If the parent class and the subclass know each other (i.e., the parent class is the base class of the subclass or the subclass is the parent derived class), the friend function of the parent class can access the private members of the subclass.
  • The friend function of the subclass cannot access the private members of the parent class: On the contrary, the friend function of the subclass cannot access the private members of the parent class, even if the parent Classes and subclasses know each other.

Practical Case

Consider the following example code:

#include <iostream>

class Base {
  friend void print(Base& b);  // 父类友元函数
private:
  int x;
};

class Derived : public Base {
  friend void access(Derived& d);  // 子类友元函数
private:
  int y;
};

void print(Base& b) { std::cout << b.x << std::endl; } // 父类友元函数访问私有成员 x

void access(Derived& d) { std::cout << d.x << " " << d.y << std::endl; } // 子类友元函数访问私有成员 x 和 y

int main() {
  Base b;
  b.x = 10;
  print(b);  // 输出:10

  Derived d;
  d.x = 20;
  d.y = 30;
  access(d);  // 输出:20 30
  print(d);  // 输出:20
}

In this example:

    Parent Class
  • Base has a friend function print(), which has access to x private members.
  • Subclass
  • Derived has a friend function access(), which can access the parent class private members x.
  • The object
  • d of the subclassDerived can be accessed by the parent class Base friend function print(), but cannot Access the private member x of the parent class.

The above is the detailed content of What impact do friend functions have on class inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn