Home >Backend Development >C++ >Handling of references and pointers in C++ function overloading and rewriting

Handling of references and pointers in C++ function overloading and rewriting

王林
王林Original
2024-04-20 11:51:01416browse

When a function is overloaded, a reference or pointer is passed, and all functions must use the same type of data. When a function is rewritten, for reference member functions, the subclass function must refer to the same type of variable; for pointing to member functions, the subclass function must point to the same type of variable.

C++ 函数重载和重写中引用和指针的处理

Handling of references and pointers in C function overloading and rewriting

Function overloading

Function overloading is allowed in the same role Create multiple functions within a domain with the same name but different parameter lists. For references and pointers, we need to pay attention to the following points:

  • When passing references, all functions must refer to the same type of data:

    void print(const int& num);
    void print(const double& d);
  • When passing pointers, all functions must point to the same type of data:

    void print(int* num);
    void print(double* d);

Function override

Function override Is to implement a function in the parent class with the same name and parameter list in the subclass. For references and pointers, we need to pay attention:

  • For reference member functions, the subclass function must refer to the same type of variable:

    class Parent {
    public:
      void setAge(const int& age);
    };
    
    class Child : public Parent {
    public:
      void setAge(const int& age) override;  // 同一类型引用
    };
  • For pointing to member functions, subclass functions must point to variables of the same type:

    class Parent {
    public:
      int* getPtr();
    };
    
    class Child : public Parent {
    public:
      int* getPtr() override;   // 同一类型指针
    };

Practical case

Assume we have A Person class that represents a person's age and name. There are two member functions in this class: setAge(const int& age) and setName(const string& name).

We create a subclass Employee, inherit from the Person class, and override the setAge function to set the age to Add 5 to it before age.

class Person {
public:
    void setAge(const int& age);
    void setName(const string& name);
};

class Employee : public Person {
public:
    void setAge(const int& age) override;  // 重写setAge函数
};

// Employee 类中的 setAge 函数
void Employee::setAge(const int& age) {
    Person::setAge(age + 5);  // 调用父类setAge函数
}

Now, we can create an Employee object and use the overridden setAge function:

Employee emp;
emp.setAge(20);  // 实际设置为 25

The above is the detailed content of Handling of references and pointers in C++ function overloading and rewriting. 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