标签: operator-keyword

如何在 C# 3.5 的超类中定义强制转换运算符?

我有一个容器类,用于向标准数据类型(如 int、string 等)添加一些属性。这个容器类封装了这样一个(标准类型)对象的对象。然后其他类使用容器类的子类来获取/设置添加的属性。现在我希望子类可以在其封装对象和自身之间隐式转换,而无需在其中添加额外的代码。

这是我的课程的简化示例:

  // Container class that encapsulates strings and adds property ToBeChecked
  // and should define the cast operators for sub classes, too.
  internal class StringContainer
  {
    protected string _str;

    public bool ToBeChecked { get; set; }

    public static implicit operator StringContainer(string str)
    {
      return new StringContainer { _str = str };
    }

    public static implicit operator string(StringContainer container)
    {
      return (container != null) ? container._str : null;
    }
  }

  // An example of many sub classes. Its …
Run Code Online (Sandbox Code Playgroud)

c# casting operator-keyword

5
推荐指数
1
解决办法
1004
查看次数


Java是否支持运算符重载?

我正在开发一个具有名为Vector2的对象的项目

public static class Vector2 {
        public Vector2 (float x, float y) {
            this.x = x;
            this.y = y;
        }

        public float x;
        public float y;

        public static Vector2 ZERO = new Vector2 (0, 0);
        public static Vector2 FORWARD = new Vector2 (0, 1);
        public static Vector2 LEFT = new Vector2 (1, 0);

        public static float Distance (Vector2 a, Vector2 b) {
            return (float) Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
        }
    }
Run Code Online (Sandbox Code Playgroud)

并希望做到以下几点:

Vector2 a = new …
Run Code Online (Sandbox Code Playgroud)

java object operator-keyword

5
推荐指数
1
解决办法
577
查看次数

C++中'和','或'等关键字的目的是什么?

以下关键字的目的是什么?

and      bitand   compl   not_eq   or_eq   xor_eq
and_eq   bitor    not     or       xor
Run Code Online (Sandbox Code Playgroud)

如果他们只是直接相当于:

&&       &        ~       !=       |=      ^=
&=       |        !       ||       ^
Run Code Online (Sandbox Code Playgroud)

c++ operators keyword operator-keyword

5
推荐指数
2
解决办法
279
查看次数

在LINQ to XML中的F#和显式转换

在C#我可以表达这个:

var xe = XElement.Parse("<foo></foo>");
var maybe = (bool?)xe.Element("bar");
Run Code Online (Sandbox Code Playgroud)

如何在F#中表达?

编辑:我确实找到了这个辅助函数

let inline conv (x : ^a) : ^b = ((^a or ^b) : (static member op_Explicit : ^a -> ^b) (x))
Run Code Online (Sandbox Code Playgroud)

xelement f# linq-to-xml operator-keyword explicit-conversion

5
推荐指数
1
解决办法
327
查看次数

C++中的迭代器

我正在尝试创建自己的翻译器.这是大学的工作.我的班级译者需要一个迭代器.

class Translator
{
private:
    map <string,Word> translator;

public:
    class iterator
    {
       friend class Translator;
        private:
            map<string,Word>::iterator itm;

        public:
            iterator operator++();
            pair <string,Word> &operator*();
            bool operator==(const iterator &it)const;
    };
};
Run Code Online (Sandbox Code Playgroud)

我想超载operator*();

这是代码.

pair <string, Word>& Translator::iterator::operator*()
{
  return (*itm);
}
Run Code Online (Sandbox Code Playgroud)

错误:

invalid initialization of reference of type ‘std::pair<std::basic_string<char>, Word>&’ from expression of type ‘std::pair<const std::basic_string<char>, Word>

c++ iterator overloading operator-keyword c++11

5
推荐指数
1
解决办法
447
查看次数

C++ 中两个大数的模

我有一个名为 的类LargeNum,它通过数组存储大量数字,例如digit[]。因为int不够大,无法存放。

\n\n

基数为10000,因此数字的'9876 8764 7263'存储方式为\xef\xbc\x9a

\n\n
digit[4] = {9876, 8764, 7263};\n
Run Code Online (Sandbox Code Playgroud)\n\n

(底数可以改为10100digit[12] = {9,8,7,6,8,7,6,4,7,2,6,3}

\n\n

问题是我想重载运算符%,这样 I\xe3\x80\x80 就可以得到两个大数的余数。大数之间的重载运算符*-通过处理大数的每一位来完成的。但我真的不知道该怎么做%。喜欢:

\n\n
{1234,7890,1234} % {4567,0023}\n
Run Code Online (Sandbox Code Playgroud)\n\n

谁能帮我?

\n

c++ overloading operator-keyword

5
推荐指数
1
解决办法
1865
查看次数

operator overloading [] [] 2d array c ++

我有一个2D数组,我想定义一个函数,它返回用户使用运算符重载的索引值.换一种说法:

void MyMatrix::ReturnValue()
{
    int row = 0, col = 0;
    cout << "Return Value From the last Matrix" << endl;
    cout << "----------------------------------" << endl;
    cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl;
}
Run Code Online (Sandbox Code Playgroud)

该操作((*this).matrix)[row][col]应该返回一个int.我不知道如何建立operator [][].
或者,我可以连接几个调用operator [],但我没有成功,因为第一次调用该操作将返回int*,第二次将返回int,它强制构建另一个运算符,我不想去做.

我能做什么?谢谢,

c++ arrays 2d operator-overloading operator-keyword

5
推荐指数
2
解决办法
3315
查看次数

如何在XML中添加OR运算符?

我找到了一些关于如何在 XML 中实现AND,less thangreater than运算符以进行 Android 数据绑定的资源。

&->&amp;

<->&lt;

>->&gt;

但是,如何添加OR运算符呢?我在Android官方文档中找不到相关信息。

我访问过这些网站

StackOverflow --- 使用“&&”逻辑运算符的android数据绑定 - 代码日志

StackOverflow --- android-databinding-using-逻辑运算符

Android 数据绑定中的转义 And(&&) 运算符

我的预期结果:

<View
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="@{viewModel.fooBoolOne ||  viewModel.fooBoolTwo ? View.VISIBLE : View.GONE}"/>```
Run Code Online (Sandbox Code Playgroud)

xml data-binding android operator-keyword android-databinding

5
推荐指数
1
解决办法
5343
查看次数

数组冒号运算符的怪异行为

我正在寻找找到的Octave函数的代码,并且发现了冒号运算符的怪异用法。我找不到文档或MathWorks官方博客(例如Colon Operator)中解释的这种行为。

假设我们有两个向量:

>> a=[1,2,3]
a =
   1   2   3
>> b=[7,8,9]
b =
   7   8   9
Run Code Online (Sandbox Code Playgroud)

现在,如果使用冒号运算符,您将:

>> a:b
ans =
   1   2   3   4   5   6   7
Run Code Online (Sandbox Code Playgroud)

经过几次尝试,我了解到上述用法等同于:

>> a(1):b(1)
ans =
   1   2   3   4   5   6   7
Run Code Online (Sandbox Code Playgroud)

我的假设正确吗?
某处有一些文档吗?

arrays matlab octave colon operator-keyword

5
推荐指数
1
解决办法
71
查看次数