Home > Backend Development > C++ > The relationship between C++ function parameter passing methods and class inheritance

The relationship between C++ function parameter passing methods and class inheritance

PHPz
Release: 2024-04-12 11:06:02
Original
799 people have browsed it

Question: What is the relationship between C function parameter passing methods and class inheritance? Answer: When a subclass inherits a parent class function, the parameter passing method can be the same or different. If the subclass does not override the parent class function, it inherits the parent class's parameter passing method. If a subclass overrides a parent class function, it can choose to use a different parameter passing method. When a subclass needs to modify parameters in a parent class function, it needs to declare the parent class function as pass-by-reference.

C++ 函数参数传递方式与类继承的关系

C The relationship between function parameter passing method and class inheritance

Introduction

There are two ways to pass function parameters in C: pass by value and pass by reference. When a parent class function is overridden by a child class, the parameter passing method may be affected. This article will explore the relationship between function parameter passing methods and class inheritance.

Pass by value

Pass by value makes a copy of the parameters when the function is called, so that modifications to the parameters within the function will not affect the actual parameters in the calling function.

void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}

int main() {
  int x = 1, y = 2;
  swap(x, y); // x 和 y 的值不会改变
  return 0;
}
Copy after login

Pass by reference

Pass by reference directly uses the address of the parameter storage when the function is called, allowing the function to directly modify the actual parameters in the calling function.

void swap(int& a, int& b) {
  int temp = a;
  a = b;
  b = temp;
}

int main() {
  int x = 1, y = 2;
  swap(x, y); // x 和 y 的值被交换
  return 0;
}
Copy after login

Class inheritance and parameter passing

When a subclass inherits a parent class function, the parameter passing method of the subclass function can be the same or different from the parent class function.

Example of unchanged parameter passing method

If the subclass function does not override the parent class function, the subclass function will inherit the parameter passing method of the parent class function.

class Base {
public:
  void func(int a);
};

class Derived: public Base {
public:
  using Base::func; // 不覆盖 func,继承按值传递
};
Copy after login

Example of changing parameter passing method

If a subclass function overrides a parent class function, the subclass function can choose to use a different parameter passing method.

class Base {
public:
  void func(int a) {}
};

class Derived: public Base {
public:
  void func(int& a) {} // 覆盖 func,使用按引用传递
};
Copy after login

Practical case

Consider a Shape base class with a print() function and an inheritance Shape and a Circle subclass with the draw() function. If Shape::print() passes a Shape reference by value, Circle::draw() will not be able to modify the parent class's print( ).

class Shape {
public:
  virtual void print(const Shape& shape) {}
};

class Circle: public Shape {
public:
  void draw() {
    // 无法修改父类的 print()
    print(static_cast<Circle&>(*this));
  }
};
Copy after login

In order to allow subclasses to modify the parent class's print(), print() needs to be declared as pass-by-reference.

class Shape {
public:
  virtual void print(Shape& shape) {}
};

class Circle: public Shape {
public:
  void draw() {
    // 可以修改父类的 print()
    print(*this);
  }
};
Copy after login

The above is the detailed content of The relationship between C++ function parameter passing methods and class inheritance. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template