从同一类运算符重载运算符是可能的吗? [英] Overloading Operator From Same Slass of operator is that possible ?

查看:97
本文介绍了从同一类运算符重载运算符是可能的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相同类的操作符和重载操作符来自同一类操作符可能在我的场景中,在下面的代码中查看注释的提及



  class 计划
{
static void Main( string [] args)
{
dem d1,d2,d3,d4;
d1 = new dem();
d1.i = 2 ;
d2 = new dem();
d2.i = 4 ;
d3 = dem.cr(d1,d2);
Console.WriteLine(d3.i);
d4 = d3 + d2;
Console.WriteLine(d4.i);
int i = 4 ,b = 4 ,n;
n = i + b;
Console.WriteLine(n);
Console.ReadLine();
}
// 此处运算符重载不起作用
public static int operator +( int l, int m)
{

int mm = l + m + 2;
返回 mm;
}
}
class dem
{
public 静态 int con = 0 ;
public int i;
public static dem cr(dem a,dem b)
{
dem dp = new dem();
dp.i = a.i + b.i;
return dp;
}
// 运算符重载正常
public static dem operator +(dem bb,dem pp)
{
dem lp = new dem();
lp.i = bb.i + pp.i;
return lp;
}
}

解决方案

您只能在定义它们的类或结构中定义运算符。



对于二元运算符,两个操作数中至少有一个必须是声明类型。



例如,这可能有效:



  class 计划{
public static int operator +(程序lhs, int rhs){
/ / ...
}

//

public static int operator +( int lhs,程序rhs){
// ...
}
}





但我是很确定这不是你想要/需要的。所以,回答你的问题,不,你不能重新定义两个整数之间的加号运算符。您只能为其定义的类或结构定义运算符。


阅读教程部分中的注释,位于 https://msdn.microsoft.com/en-us/library/aa288467(v = vs.71 ).aspx [ ^ ]。

Operator Of Same Class And Overloading Operator From Same Slass of operator is that possible In My Scenario , Check Out Comment's Mention In Code Below

class Program
    {
        static void Main(string[] args)
        {
            dem d1, d2, d3, d4;
            d1 = new dem();
            d1.i = 2;
            d2 = new dem();
            d2.i = 4;
          d3=dem.cr(  d1 , d2);
          Console.WriteLine(d3.i);
          d4 = d3 + d2;
          Console.WriteLine(d4.i);
          int i=4, b=4, n;
          n = i + b;
          Console.WriteLine(n);
          Console.ReadLine();
    }
        // Here Operator Over Loading Does Not Work 
        public static int operator+ (int l , int m)
        {

            int mm = l + m+2;
            return mm;
        }
    }
    class dem
    {
        public static int con=0;
        public int i;
        public static dem cr(dem a,dem b)
        {    
         dem dp = new dem();
         dp.i = a.i + b.i;
            return dp;
        }
        // Operator Overloading works correctly 
        public static dem operator+(dem bb , dem pp)
        {
            dem lp = new dem();
            lp.i=bb.i + pp.i;
            return lp;
        }
    }

解决方案

You can only define operators in the class or structure for which they are defined.

For binary operators, at least one of the two operands must be of the declaring type.

For example, that could work:

class Program {
   public static int operator +(Program lhs, int rhs) {
      // ...
   }

// OR

   public static int operator +(int lhs, Program rhs) {
      // ...
   }
}



But I'm pretty sure that's not what you want/need. So, to answer your question, no, you cannot redefine yourself the plus operator between two integers. You can only define an operator for the class or structure it is defined in.


Read the comments in the section headed Tutorial, at https://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx[^].


这篇关于从同一类运算符重载运算符是可能的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆