80分:如何使用C#代码改写C++重写运算符的代码?

WTPMCheng 2010-10-14 10:53:16
近日要重写一段C++写的代码,但是不知道在c#中怎么重写一下代码?

看怎么重写以下代码?先谢谢。

能给我解释下重写运算符更好,还有PointStruct& abc这段代码 在C#中该怎么写?

class PointStruct
{
public:
int Point_ID;
double x;
double y;
double z;
bool b_EdgePoint;
void operator=( PointStruct& abc)
{
Point_ID=abc.Point_ID;
x=abc.x;
y=abc.y;
z=abc.z;
b_EdgePoint = abc.b_EdgePoint;
}

bool operator==(PointStruct& rhs) const {
return (x == rhs.x &&
y == rhs.y &&
z == rhs.z &&
Point_ID == rhs.Point_ID&&
b_EdgePoint==rhs.b_EdgePoint
);
}


PointStruct(PointStruct& abc)
{
Point_ID=abc.Point_ID;
x=abc.x;
y=abc.y;
z=abc.z;
b_EdgePoint = abc.b_EdgePoint;
}

}
...全文
176 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
symbol_bc 2010-10-14
  • 打赏
  • 举报
回复

public override bool Equals(object obj)
{
PointStruct ps = obj as PointStruct;

return this.x == ps.x && this.y == ps.y && this.z == ps.z && this.b_EdgePoint == ps.b_EdgePoint;
}
WTPMCheng 2010-10-14
  • 打赏
  • 举报
回复
public class PointStruct : ICloneable
{
public int Point_ID;
public double x;
public double y;
public double z;
bool b_EdgePoint;

//这里会报错:没有找到合适方法重写
public override bool Equals(PointStruct abc)
{
Point_ID = abc.Point_ID;
x = abc.x;
y = abc.y;
z = abc.z;
b_EdgePoint = abc.b_EdgePoint;
}

public override int GetHashCode()
{

}

public static bool operator ==(PointStruct rhs, PointStruct rhs1)
{
return Object.Equals(rhs, rhs1);
}

public static bool operator !=(PointStruct a, PointStruct b)
{
return !Object.Equals(a, b);

}

#region ICloneable 成员

object ICloneable.Clone()
{
throw new NotImplementedException();
}

#endregion
}

我现在大致写了下,不知道对不对??
WTPMCheng 2010-10-14
  • 打赏
  • 举报
回复
LS的朋友:
bool operator==(PointStruct& rhs)

重载==后 不是应该需要两个"参数"吗,但是现在的C++代码 只有一个 那我该怎么改写?

拷贝的构造函数 不是很懂。

按照我上面的例子举个例子更好
wuyq11 2010-10-14
  • 打赏
  • 举报
回复
List<PointStruct> PointStruct_Array
wuyq11 2010-10-14
  • 打赏
  • 举报
回复
operator 运算符,如+,-,*,/,==,!=,>,<等

public static bool operator ==(T a, T b)
{
if (a.Name== b.Name && a.Age == b.Age)
{
return true;
}
else
{
return false;
}
}
兔子-顾问 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wtpmcheng 的回复:]
还有一个小问题:static CArray <PointStruct,PointStruct&> PointStruct_Array;
这句代码:使用List<PointStruct> PointStruct_Array; 可以吗?
[/Quote]
static List<PointStruct> PointStruct_Array;
兔子-顾问 2010-10-14
  • 打赏
  • 举报
回复
=不支持重载操作符。你需要实现ICloneable接口
==可以重载
拷贝构造函数,就ICloneable就可以了。
WTPMCheng 2010-10-14
  • 打赏
  • 举报
回复
还有一个小问题:static CArray <PointStruct,PointStruct&> PointStruct_Array;
这句代码:使用List<PointStruct> PointStruct_Array; 可以吗?
风骑士之怒 2010-10-14
  • 打赏
  • 举报
回复
这是拷贝构造函数

构造函数是一种特殊函数,而拷贝构造函数是一种特殊的构造函数。类X的构造函数的第一个参数必须为X&,或者const X&;除了第一个参数外,构造函数要么不存在其他参数,如果存在其他参数,其他参数必须有默认值。一个类可以有多个拷贝构造函数。

C++新手,也不太懂。。
ysz89757 2010-10-14
  • 打赏
  • 举报
回复
学习下

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧