CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    54

    [RESOLVED] Operator overloading - In class or global?

    I have two classes vmSize and vmPoint. Both have operator overloading functions, but mine are within the class. Looking at another developers implementation of both these classes, theirs are global?

    I do not understand what the benefits are making them global, can someone explain this to me?

    I do wonder if it has something to do with the fact that their Point class has an overloaded operator that takes a Size object?

    i.e.

    Point operator+ (const Point &pt, const Size &sz)
    What the mind can conceive it can achieve.

  2. #2
    Join Date
    Jan 2015
    Posts
    16

    Re: Operator overloading - In class or global?

    You may be interested in this article by Scott Meyers.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: Operator overloading - In class or global?

    If the lhs of the operator does not refer to the same class, then it has to be non-member.

    eg for a class A, A+B can be either member or non-member but B+A needs to be non-member. When there is a choice of using member or non-member see the article referred to in jlb1's post #2.

    PS How class functions are implemented (member vs non-member etc) really should be documented as part of in-house coding standards.
    Last edited by 2kaud; November 23rd, 2016 at 10:23 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured