Return multiple values to a method caller

前端 未结 26 2303
故里飘歌
故里飘歌 2020-11-21 21:57

I read the C++ version of this question but didn\'t really understand it.

Can someone please explain clearly if it can be done and how?

26条回答
  •  离开以前
    2020-11-21 22:50

    Just use in OOP manner a class like this:

    class div
    {
        public int remainder;
    
        public int quotient(int dividend, int divisor)
        {
            remainder = ...;
            return ...;
        }
    }
    

    The function member returns the quotient which most callers are primarily interested in. Additionally it stores the remainder as a data member, which is easily accessible by the caller afterwards.

    This way you can have many additional "return values", very useful if you implement database or networking calls, where lots of error messages may be needed but only in case an error occurs.

    I entered this solution also in the C++ question that OP is referring to.

提交回复
热议问题