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

Thread: operator overloading

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    operator overloading

    hey guys,
    I`m trying to overload operator = in my class in windows application`s form , I`m not sure what is my problem,would you take a look at my code?

    public struct Customer
    {
    // Public members
    // public string Name;
    public string Name;
    public string ID;
    public string Address;
    public string Hour;


    public Customer operator +(Customer ob)
    {
    this.Name = ob.Name;
    this.ID = ob.ID;
    this.Address = ob.Address;
    this.Hour = ob.Hour;
    return *this;
    }
    }

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

    Re: operator overloading

    C# isn't C++. You can't use pointers like that.

    Also, this is completely unnecessary. It's a struct, it's a value type and therefore it's copied by default.

    [code]
    Customor c = new Customer { Name = "Ted" };
    Customer d = c;
    c.Name = "Bob";
    Console.WriteLine (d.Name); <-- Prints Ted.
    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
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: operator overloading

    you should use operator= overloading when you are concern about Deep Copy and Shallow Copy
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

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

    Re: operator overloading

    Do you mean override the assignment operation '=' because that can't be done in C#. If you're concerned about deep copy versus shallow copy, you should be implementing a 'Clone' method.
    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.

  5. #5
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: operator overloading

    i mean overloading...we don't have such thing as operator overriding (as you mentioned).
    in his question he asked for = overloading that is possible in c++ but in his code he used + overloading that is possible both in c++ and C#.

    btw his code is unclear:

    Code:
    public Customer operator +(Customer ob)
    above code in C# is like to overload unary + operator

    but
    Code:
      static public Object operator+ ( Object lhs, Object rhs)
    above code is for overloading Binary + operator that is completely different.

    anyway his question is not clear also in c++ we can overload Binary + like this:

    Code:
    const Object operator+(const Object& obj) const;
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

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

    Re: operator overloading

    in his question he asked for = overloading that is possible in c++ but in his code he used + overloading that is possible both in c++ and C#.
    I figured he had a typo in his question, what with + and = being the same key. I assume he's looking for + because that's what the code was demonstrating.

    Code:
    	public class Customer
    	{
    		public string Name;
    		public int Age;
    
    		public Customer operator +(Customer other)
    		{
    			Name = other.Name;
    			Age = other.Age;
    		}
    	}
    This is invalid C#. First you have to declare it static, secondly (most importantly) the '+' operator needs two operands. The above won't compile.

    Code:
    	public struct Customer
    	{
    		public string Name;
    		public int Age;
    		
    		public static Customer operator +(Customer left, Customer right)
    		{
    			return new Customer {
    				Name = left.Name + right.Name,
    				Age = left.Age + right.Age
    			};
    		}
    	}
    The above will compile in C# and is the only way to overload the '+' operator. However, it makes no sense. You can't logically 'add' a customer. What you want is:

    Code:
    	public struct Customer
    	{
    		public string Name;
    		public int Age;
    		
    		public Customer Clone ()
    		{
    			return new Customer {
    				this.Name,
    				this.Age,
    			};
    		}
    	}
    This will implement what he wants in the normal way which is easily understandable by all C# developers. Of course, implementing Clone as I did above on a struct is a complete waste of time as I outlined already. Structs already give you this for free.
    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.

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