如何在不添加/删除mem函数的const常数的情况下重载getter函数?

Con*_*nst -2 c++ overloading member-functions c++17

我无法弄清楚,为什么C ++不允许根据返回类型进行重载,因为在以下情况下,三个member(getter)函数具有不同的函数签名,即使在存储指向成员函数的指针时,我们也需要不同的mem-函数指针类型如下:

for instance T = std::string
using constRefPtr   = const std::string&(MyStruct::*)() const;
using constValuePtr = const std::string(MyStruct::*)() const;
using valuePtr      = std::string(MyStruct::*)() const;
Run Code Online (Sandbox Code Playgroud)

我已经阅读过这篇类似的文章,建议使用const和非成本成员函数。

问题:如何在不删除const每个成员函数的本质的情况下使以下(getter)重载工作(如果可以通过标准C ++实现)?

我正在使用C ++ 17。

#include <iostream>
#include <string>

template<typename T> class MyStruct
{
    T m_val;
public: 
    explicit MyStruct(const T& value) 
        : m_val(value)
    {}
    const T& getVal() const {   return m_val; } // get val as const ref(no copy of member)
    const T getVal() const  {   return m_val; } // get a const member as return
    T getVal() const        {   return m_val; } // get a copy of member
};

int main()
{
    MyStruct<std::string> obj{"string"};
    const auto& val_const_ref = obj.getVal();  // overload const std::string& getVal() const
    const auto val_const = obj.getVal();       // overload const std::string getVal()  const
    auto val = obj.getVal();                   // overload std::string getVal()  const
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息:

error C2373 : 'MyStruct<T>::getVal' : redefinition; different type modifiers
note: see declaration of 'MyStruct<T>::getVal'
note: see reference to class template instantiation 'MyStruct<T>' being compiled
error C2059 : syntax error : 'return'
error C2238 : unexpected token(s) preceding ';'
error C2143 : syntax error : missing ';' before '}'
error C2556 : 'const T MyStruct<T>::getVal(void) const' : overloaded function differs only by return type from 'const T &MyStruct<T>::getVal(void) const'
1 > with
1 > [
    1 > T = std::string
        1 > ]
    1 > C:\Z Drive\CPP Programs\Visual Studio Project\Main.cc(62) : note: see declaration of 'MyStruct<std::string>::getVal'
note: see reference to class template instantiation 'MyStruct<std::string>' being compiled
error C2373 : 'MyStruct<std::string>::getVal' : redefinition; different type modifiers
note: see declaration of 'MyStruct<std::string>::getVal'
error C2059 : syntax error : 'return'
error C2238 : unexpected token(s) preceding ';'
error C2146 : syntax error : missing ';' before identifier 'T'
error C2530 : 'val_const_ref' : references must be initialized
error C2789 : 'val_const' : an object of const - qualified type must be initialized
note: see declaration of 'val_const'
Run Code Online (Sandbox Code Playgroud)

Bar*_*rry 5

您只是...不能在返回类型过载,句号停止。

您可以仅创建两个名称不同的函数:

T const& ref() const { return m_val; }
T val() const        { return m_val; }
Run Code Online (Sandbox Code Playgroud)

根据ness或ness 可以重载哪些自身const&

T const& ref() const { return m_val; }
T&       ref()       { return m_val; }

T val() const&       { return m_val; }
T val() &&           { return std::move(m_val); }
Run Code Online (Sandbox Code Playgroud)

  • 或者可能没有琐碎的吸气剂/装料器?问题解决了 :) (3认同)
  • 您可以添加示例代码来显示btw`val()`之间的差异吗?我认为那会有所帮助 (2认同)