Home > Backend Development > C++ > Why can't C functions be overloaded based solely on the constness of a non-pointer, non-reference argument?

Why can't C functions be overloaded based solely on the constness of a non-pointer, non-reference argument?

Patricia Arquette
Release: 2024-11-11 21:25:03
Original
431 people have browsed it

Why can't C   functions be overloaded based solely on the constness of a non-pointer, non-reference argument?

Overloading Functions with Const Arguments: Why It Cannot Be Done

In C , overloading a function allows for multiple functions with the same name but different parameter lists to coexist within a class. However, one cannot overload functions solely based on the constness of a non-pointer, non-reference parameter.

Consider the following code example:

#include <iostream>
using std::cout;

class Test {
public:
    Test() {}
    int foo(const int) const;    // const function
    int foo(int);                // non-const function
};

int main() {
    Test obj;
    Test const obj1;
    int variable = 0;
    do {
        obj.foo(3); // Call the const function
        obj.foo(variable); // Attempt to call the non-const function
        variable++;
        usleep(2000000);
    } while (1);
}

int Test::foo(int a) {
    cout << "NON CONST" << std::endl;
    a++;
    return a;
}

int Test::foo(const int a) const {
    cout << "CONST" << std::endl;
    return a;
}
Copy after login

As evident in the code, we attempt to overload the foo method based on the constness of the argument: a const version that accepts const arguments and a non-const version that accepts non-const arguments. However, this code will result in a compilation error stating that the function cannot be overloaded.

This error arises because C does not allow overloading based only on the constness of a non-pointer, non-reference type. The reason is that when passing a value argument, the actual argument is copied into a temporary variable inside the called function. This copy is distinct from the original argument, and its constness does not affect the constness of the original argument.

Therefore, in the example above, when calling obj.foo(3), a temporary copy of the literal 3 is created, and it is passed to the const function. Similarly, when calling obj.foo(variable), a temporary copy of variable is created and passed to the non-const function. The compiler cannot differentiate between these two cases based solely on the constness of the argument.

In summary, overloading functions based on the constness of a non-pointer, non-reference type is not permitted in C . This is because the constness of a value argument does not affect the constness of the argument inside the function, and the compiler cannot distinguish between the two based on the argument type alone.

The above is the detailed content of Why can't C functions be overloaded based solely on the constness of a non-pointer, non-reference argument?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template