简体   繁体   中英

C++ Compilation error in overloading

The following code compiles fine.

#include <iostream>
#include <vector>
using namespace std;

class MyClass
{
public:
    MyClass()
    {
        x.resize(2);
        x[0] = 10;
        x[1] = 100;
    }
    std::vector<int> getValue()
    {
        return x;
    }
    const std::vector<int>& getValue() const
    {
        return x;
    }
private:
       std::vector<int> x;
};


int main()
{

    MyClass m;
    std::vector<int> y = m.getValue();
    for(int i=0; i<y.size(); i++)
    {
        std::cout<<y[i]<<std::endl;
    }

    const std::vector<int>& z = m.getValue();
    for(int i=0; i<z.size(); i++)
    {
        std::cout<<z[i]<<std::endl;
    }
    return 0;
}

However, when I change the "std::vector getValue()" to a more correct version (since the function is supposed to change the object) by adding "const" (std::vector getValue() const) it gives the following compilation error.

error: 'const std::vector<int>& MyClass::getValue() const' cannot be overloaded const std::vector<int>& getValue() const

Why is it so?

I have used "gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)"

You can't define two functions with the same name, which differ only in return type. So define function with different name, for example:

std::vector<int> getValueCopy() const;

By adding const to the first function you render calls to getValue ambiguous: What is the difference between those 2 functions:

std::vector<int> getValue() const;        // 1
const std::vector<int>& getValue() const; // 2

Well, they are the same, except for the return value, but wait! You can't overload based on the return type in C++! It wouldn't make sense, most calls would be ambiguous:

std::vector<int> y = m.getValue(); // which one? It can be 1, it can be 2 (std::vector<int>
                                   // is not a better match than const std::vector<int>&)

const std::vector<int>& z = m.getValue(); // same as above

m.getValue(); // which one?

But also, what is supposed to be the difference between the two?

The first one is 100% safe, while the second one is not: one could store a reference to x , and it becomes a dangling reference if the object gets destroyed. And so I would say that you get rid of the second one, if possible.

your problem is that you didn't understand functions overloading concept

when you overload function The definition of the function must differ from each other by the types of argument or the number of arguments in the argument list.

You can not overload function declarations that differ only by return type.

in your functions :

std::vector<int> getValue() const 

const std::vector<int>& getValue() const

it only differ in return type so it will be not considered as overloaded functions

best way to correct your error is to change your second function name to getValuev2()

or change arguments of one of the functions.

you can read more about overloading in C++ : https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM