Home > Backend Development > C++ > body text

C++ function call object-oriented design: parameter passing and return value object passing

WBOY
Release: 2024-04-30 16:06:01
Original
429 people have browsed it

In a C function call, parameters can be passed by value (receiving a copy of the parameter) or by reference (receiving a reference to the actual object). Return values ​​can also be passed by value or by reference. Passing by value copies the object, while passing by reference passes a reference to the object, thus affecting the actual object.

C++ 函数调用面向对象设计:参数传递和返回值的对象传递

C Object-oriented design of function calls: parameter passing and object passing of return values

Introduction
In object-oriented programming, the function calling mechanism plays a crucial role. When calling a class method or function, parameters and return values ​​can be passed by value or by reference. This article will deeply explore the object passing mechanism of parameter passing and return value in C, and demonstrate it through practical cases.

Parameter passing

In C, parameters can be passed to functions by value or by reference. Passing by value means that the function receives a copy of the argument, while passing by reference means that the function receives a reference to the actual object.

  • Value transfer:

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

The above swap() function pairs two integers An exchange was made. Since a and b are passed by value, modifications to them inside the function will not affect the actual variables outside the function.

  • Pass by reference:

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

The modified swap() function uses pass by reference , which means that modifications to a and b inside the function will be propagated back to the outside of the function.

Object passing of return value

Function can not only receive objects through parameters, but also return objects through return values. Likewise, objects can be returned by value or by reference.

  • Value passing:

    Point getPoint() {
    return Point(x, y); // 复制构造函数被调用
    }
    Copy after login

##getPoint() The function returns a Point Object. Since function return values ​​are passed by value, objects constructed inside the function will be copied outside the function.

  • Pass by reference:

    Point& getPoint() {
    return Point(x, y); // 返回对临时变量的引用
    }
    Copy after login

The above modified

getPoint() function returns by References to temporary variables implement pass-by-reference. Therefore, a reference to the actual object can be obtained outside the function. However, it should be noted that if the function returns a reference to a local variable, the local variable will be destroyed when the function returns, and the reference will become invalid.

Practical case

Consider the following code:

class MyClass {
public:
  MyClass(int x, int y) : x(x), y(y) {}
  void print() {
    std::cout << "x: " << x << ", y: " << y << std::endl;
  }
private:
  int x, y;
};

int main() {
  MyClass obj1(10, 20); // 对象以值传递创建
  MyClass obj2; // 默认构造
  
  // 参数传递演示
  std::cout << "Before swap:" << std::endl;
  obj1.print();
  swap(obj1, obj2);
  std::cout << "After swap:" << std::endl;
  obj1.print();
  obj2.print();
  
  // 返回值传递演示
  MyClass obj3 = getPoint(); // 对象以值传递返回
  MyClass& obj4 = getPoint(); // 对象以引用传递返回
  
  // 对象修改
  obj3.x++;
  obj4.y--;
  
  // 输出结果
  std::cout << "obj3:" << std::endl;
  obj3.print();
  std::cout << "obj4:" << std::endl;
  obj4.print();
  
  return 0;
}
Copy after login

Result:

Before swap:
x: 10, y: 20
After swap:
x: 10, y: 20
x: 10, y: 20
obj3:
x: 11, y: 20
obj4:
x: 10, y: 19
Copy after login
In this case , the value transfer in parameter transfer will not affect the actual object outside the function, while the reference transfer directly modifies the actual object. For return values, passing by value creates a copy object, while passing by reference returns a reference to the actual object.

The above is the detailed content of C++ function call object-oriented design: parameter passing and return value object passing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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