开发者

Operator overloading c++ placement

Im having trouble figuring out where to place the overloaded operators i created. should they be in the class they operate on, or inside? both ways are causing me trouble. any pointers would be great.

here is my very basic uint128_t class:

class uint128_t{
    private:
        uint64_t UPPER, LOWER;

    public:
    // constructors
        uint128_t(){
            UPPER = 0;
            LOWER = 0;
        }

        template <typename T>
        uint128_t(T val){
            UPPER = 0;
            LOWER = (uint64_t) val;
        }

        template <typename S, typename T>
        uint128_t(const S & upper_val, const T & lower_val){
            UPPER = (uint64_t) upper_val;
            LOWER = (uint64_t) lower_val;
        }

        uint128_t(uint128_t val){
            UPPER = val.upper();
            LOWER = val.lower();
        }

        uint64_t upper(){
            return UPPER;
        }

        uint64_t lower(){
            return LOWER;
        }
};

if the operators are in the class, they work fine. however, i can do uint128_t ^ uint32_t but not uint32_t ^ uint128_t. on the other hand, moving everything outside is giving m开发者_C百科e error: 'uint128_t operator=(uint128_t, T)' must be a nonstatic member function. also, operator= will not work for a constant input apparently, since the values will be ULLL, which doesnt exist, unless someone know of a way to do so.

what should i do?


Why not a mix of both? Put operator= inside, and the rest outside.

For the ones that take uint128_t as left-hand-side argument it doesn't matter, if it's a right-hand-side argument then it has to be a global function. So for that reason you can put both functions side-by-side if they're global. You might even get away with a clever macro to have one implementation for both, if only for the commutative ones.


In general it's better to put them outside so for a class X with an implicit constructor from type Y, a single function operator+(X&, const X&) allows you to add X's and Y's ala x + y and y + x. If you can see a way to - and wish to - make x + y and/or y + x faster than x + x then you may want separate functions anyway. One thing to keep in mind: it's often good to have an operator+= member function then implement + in terms of returning a temporary copy of the lhs (left-hand-side value) after doing a += of the rhs....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜