Overloading by return type

前端 未结 11 1731
我在风中等你
我在风中等你 2020-11-22 07:07

I read few questions here on SO about this topic which seems yet confusing to me. I\'ve just begun to learn C++ and I haven\'t studied templates yet or operator overloading

11条回答
  •  感情败类
    2020-11-22 07:59

    It's possible, but I'm not sure that it's a technique I'd recommend for beginners. As in other cases, when you want the choice of functions to depend on how the return value is used, you use a proxy; first define functions like getChar and getInt, then a generic get() which returns a Proxy like this:

    class Proxy
    {
        My const* myOwner;
    public:
        Proxy( My const* owner ) : myOwner( owner ) {}
        operator int() const
        {
            return myOwner->getInt();
        }
        operator char() const
        {
            return myOwner->getChar();
        }
    };
    

    Extend it to as many types as you need.

提交回复
热议问题