Detail C++ function overloading

  • 2020-10-07 18:50:00
  • OfStack

The nature of function overloading

In c++, 1 function is determined by the function name and function
So it's ok to have the same function name and different arguments
Unlike c, c does not have function overloading. The essential address of a function is its name
Function overloading occurs within the same scope

Overloading in a class

Constructor overload
Normal member function overloads
Static member function overloads

Can global functions, static member functions, and normal member functions be overloaded?

The essence is that the function name and function parameter are different and occur in the same scope
Static and normal member functions are fine
The global function scope is in the global scope, so it cannot be

Question 1: Does overloading occur when the member function of the parent class and the member function of the child class are equal?

The essence is the same as above, because the superclass and the subclass do not have the same scope

Look at one piece of code


 #include <iostream>

class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 void print() {
 std::cout << "father print" << std::endl;
 }
};

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
 void print(int a) {
 std::cout << "child print = " << a << std::endl;
 }
};

int main(){
 father* father_test = new father();
 father_test->print(); // print father print

 child* child_test2 = new child();
 child_test2->print(); // Compile error no matching function for call to 'child::print()'
   
 return 0;
}

According to the printout, the first print belongs to the father normal output, no problem, the second print is a compilation error, we actually want to access the parent class print But because the subclasses are defined print , so in the scope of the subclass, print Now there's only one of these functions, and that's theta void print(int a) , if you want to call the parent class to display the specified child_test2->father::print() ;

Question 2. Can subclasses override the functions of the parent class, or define the same?

Look at the code


#include <iostream>

class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 void print() {
 std::cout << "father print" << std::endl;
 }
};

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
 void print() {
 std::cout << "child print" << std::endl;
 }
};

int main(){
 father* father_test = new father();
 father_test->print(); // print father print

 child* child_test2 = new child();
 child_test2->print(); //child print

 return 0;
}

You can see that you can define the same function and rewrite it

Question 3. When global operator overloading meets operator overloading in a class, what is the priority


 #include <iostream>
class child;
class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 bool operator==(const father& e) {
 std::cout << "void print(const father& e)" << std::endl;
 }
};
bool operator==(const father& e1, const father& e2) {
 std::cout << "void print(const child& e1, const child& e2)" << std::endl;
}

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
};


int main(){
 child child1_test;
 child child2_test;

 child1_test==child2_test;
 return 0;
}

The output is void print(const father& e) Operator overload precedence in a class is greater than global

What if replication compatibility meets global overloading?


#include <iostream>
class child;
class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 bool operator==(const father& e) {
 std::cout << "void print(const father& e)" << std::endl;
 }
};
bool operator==(const child& e1, const child& e2) {
 std::cout << "void print(const child& e1, const child& e2)" << std::endl;
}

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
};


int main(){
 child child1_test;
 child child2_test;

 child1_test==child2_test;
 return 0;
}

Print:" void print(const child& e1, const child& e2) "

Global function parameter types, normally classes and global == are ok, but when a default conversion is required in a class, subclass to superclass, and the global direct type is correct, the compiler USES the global first

Speaking of operator overloading, we now define functions that implement class functions with three options: member functions, global functions, and global + friend functions

1. See if a virtual function is needed. If a virtual function is needed, it must be a member of the class

2. See if it is defined < < or > > Operator, we all know that the printout is global < < But why? Look at the actual prototype for 1 String In terms of ostream& operator<<(ostream& output, const String& string) When we use it std::cout << "haha ";
If it's defined in a class print0 To use is "haha"<<"std::cout" See the difference, if the definition is awkward to use in a class, continue with the above result, if it is < < or > > Operator, and then, if you need to access the private field, change the global function to the corresponding friend function of the class

3. When you need to cast the left argument, you need to define the global function, because the operator function in the class, the left is the class itself, if you need to access the private field, then the global function will be changed to the corresponding friend function of the class

4. Other cases are member functions of the class

The above is a detailed explanation of C++ function overload content, more information about c++ function overload please pay attention to other related articles on this site!


Related articles: