如何在C#类中转换此C ++类? [英] How I can convert this C++ class in a C# class?
问题描述
我需要一个学校项目来上一堂课.我在C ++中工作更多.我三周前开始学习C#,但我不理解C#类和C ++类的区别.
我尝试过的事情:
I need for a school project to make a poin class. I work in C++ more. I star learning c# three weeks ago and I did not understand the diference C# class and C++ class.
What I have tried:
class point{
public:
int x;
int y;
point() : x(0), y (0) {
}
point( int x, int y ){
this->x = x;
this->y = y;
}
point( const point& from ){
this->x = from.x;
this->y = from.y;
}
point& operator = ( const point& from ){
this->x = from.x;
this->y = from.y;
return *this;
}
bool operator == ( const point& from ){
return ((this->x == from.x ) && (this->y == from.y ));
}
~point() {}
};
推荐答案
忘记C ++类,并从C#角度考虑它-除非您从某个时候开始这样做,否则您将始终在C ++中思考" ",这意味着您不一定会产生良好的C#代码.
从基础开始,并创建该类的大纲:
Forget your C++ class, and think about it from a C# perspective - unless you start doing that at some point, you will always "think in C++" and that means you won''t necessarily produce good C# code.
Start with the basics, and create an outline of the class:
public class MyPoint
{
public MyPoint(int x, int y)
{
}
}
然后在其中添加两个属性X和Y,并将它们连接到构造函数中.
添加第二个构造函数以克隆现有的MyPoint.
添加一个赋值运算符.
添加比较运算符.
这并不复杂,但是C#和C ++是共享许多通用语法的不同语言.用C ++进行编码并将其转换为C#不会产生好的代码,而是会在C#的"shell"中产生C ++的代码.学习使用C#进行思考,并利用您以前的编程经验来帮助您进行设计,而不是生成代码.
Then add the two properties X and Y to it, and hook them up in the constructor.
Add a second constructor to clone an existing MyPoint.
Add an assignment operator.
Add a comparison operator.
This isn''t complicated, but C# and C++ are different languages that share a lot of common syntax. Coding in C++ and converting it to C# doesn;t produce good code, it produces C++ code in a C# "shell". Learn to think in C#, and just use your previous programming experience to help you design, not produce code.
我发现了一个不错的转换器C ++ => C#代表谁会和我处于同样的境地.
链接:下载 [
I found a good converter C++ => C# for who would be in the same situation as me.
Link: Download[^]
这篇关于如何在C#类中转换此C ++类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!