operator overloading
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: operator overloading

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    11

    Question operator overloading

    Hi guys,

    Can someone tell me in very simple code that what exactly is operator overloading and what is it mainly used for...i read a couple of articles and all explained it using multidimensional arrays and vectors and i am beginner so really could'nt get it very clearly .

    Thanks
    Sid

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: operator overloading

    Operator overloading is so you can use the standard operators (+, -, *, /) with custom types.

    Code:
    public struct ComplexNumber {
        public double Real { get; set; }
        public double Imaginary { get; set; }
    
        public static ComplexNumber operator + (ComplexNumber left, ComplexNumber right)
        {
            return new ComplexNumber {
                Real = left.Real + right.Real,
                Imaginary = left.Imaginary + right.Imaginary
            };
        }
    }
    
    var a = new ComplexNumber (1, 2);
    var b = new ComplexNumber (3, 4);
    
    // 'c' is now the complex number (4, 6)
    var c = a + b;
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: operator overloading

    Operators are defined for built in types, such as integers, floating point numbers or string. But you might also want to use operators like the arithmetic operators for your own classes. For instance the Complex class shown above, or maybe a Matrix class. You want to be able to sum or multiply to matrices. There isn't a + or * built in that is able to operate your matrices, because that's impossible. Or maybe you want to compare (<, > , !=, ==) Cars, Bounds, Clients, Stars, or whatever other types you define. So you can "overload" some operators. This means you can give them additional functionality to operate your defined classes. (operators are nothing but functions).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: operator overloading

    Also if you do overload operators for your class, make sure their meaning is obvious. For example, if a, b and c are all Strings, then:

    a = b + c; // concatenation is widely expected by coders

    whereas, if the '-' operator had been overloaded:

    a = b - c; // not obvious what the designer of the String class meant
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: operator overloading

    Yes, that is a good point. The common (natural) meaning of the operators should not be changed (should I say "overloaded"? ).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Jan 2010
    Posts
    11

    Cool Re: operator overloading

    Thanks guys,

    Yeah now i got a very good understanding of how this works .

  7. #7
    Join Date
    Dec 2009
    Posts
    22

    Re: operator overloading

    I made an app once that required some math structures like vectors and matrices. I overloaded the operators on the classes i created in order to get the same behavior in the app as in math:
    Ex:
    [1,2,3] + [1,2,3] = [2,4,6]

    where [1,2,3] is a 3d vector with the values x=1, y=2, and z=3. This made the code much easier to understand and the vectors behaved just as they would in math.

    (i hope i did the math right, its been a long time)

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center