难以理解的语法,来自 vc++到 c#

发布于 2024-12-09 13:52:10 字数 970 浏览 0 评论 0原文

我有以下代码,这对于理解我正在从 Visual C++ 转换为 C# 的类至关重要,

typedef Rect<double, 2> Rect2;

我根本无法理解它的含义。我已经了解到 vc++ 使用“stl”或“std”和“向量”类,这类似于 c# list 或 arraylist,但语法 up 完全让我困惑。

顺便说一下,矩形的定义是这样写的:

template<class Real, int Dim>
class Rect {

矩形是一个vc++项目文件中的一个类,但我无法理解这个typedef的意义是什么。它有 200 多行,所以这里是声明开始

template<class Real, int Dim>
class Rect {
public:

typedef Vector<Real, Dim> Vec;
typedef Rect<Real, Dim> Self;
typedef _RectPrivate::RectOp<Dim> RO;

Rect() : empty(true) {}
Rect(const Vec &vec) : empty(false), lo(vec), hi(vec) {}
Rect(const Vec &inLo, const Vec &inHi) : lo(inLo), hi(inHi) { markEmpty(); }
Rect(const Rect &inRect) : empty(inRect.empty), lo(inRect.lo), hi(inRect.hi) {}
template<class R> Rect(const Rect<R, Dim> &inRect) : empty(inRect.empty), lo(inRect.lo), hi(inRect.hi) {}

任何帮助表示赞赏。

I have the following code, which is vital to understand a class I'm working on translating from visual c++ to c#

typedef Rect<double, 2> Rect2;

I simply can't understand what does it mean. I have come to understand that vc++ uses 'stl' or 'std' and a 'vector' class, which is similar to c# list or arraylist, but the syntax up completely eludes me.

By the way, Rect definition is written like this

template<class Real, int Dim>
class Rect {

Rect is a class in one of the vc++ project files, but I can't understand what's the point of this typedef. It's 200+ lines so here's the declaration start

template<class Real, int Dim>
class Rect {
public:

typedef Vector<Real, Dim> Vec;
typedef Rect<Real, Dim> Self;
typedef _RectPrivate::RectOp<Dim> RO;

Rect() : empty(true) {}
Rect(const Vec &vec) : empty(false), lo(vec), hi(vec) {}
Rect(const Vec &inLo, const Vec &inHi) : lo(inLo), hi(inHi) { markEmpty(); }
Rect(const Rect &inRect) : empty(inRect.empty), lo(inRect.lo), hi(inRect.hi) {}
template<class R> Rect(const Rect<R, Dim> &inRect) : empty(inRect.empty), lo(inRect.lo), hi(inRect.hi) {}

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-12-16 13:52:10

根据您对其他答案的评论,您面临的问题是您不了解 C++ 模板是什么。它们实际上与 C# 泛型有很大不同。它们在概念上相似,语法上相似,但它们的实际实现却截然不同。

在 C++ 中考虑模板的最简单方法是将其视为“搜索和替换”机制。当你看到

typedef Rect<double, 2> Rect2;

That 意味着去查找带有两个模板参数的 Rect 的模板类。这里是:

template<class Real, int Dim>
class Rect {

好的,所以“double”对应于“class Real”,“2”对应于“int Dim”。果然,double 是类型,2 是 int,所以可以正常工作。现在检查模板类,并将“Real”的所有实例替换为“double”,将“Dim”替换为 2。结果是:

typedef /* now we start replacing double for Real and 2 for Dim */ 
class Rect {
public:
typedef Vector<double, double> Vec;
typedef Rect<double, 2> Self;
typedef _RectPrivate::RectOp<2> RO;
Rect() : empty(true) {}
Rect(const Vec &vec) : empty(false), lo(vec), hi(vec) {}
...
} Rect2;

请注意,模板本身的搜索和替换结果都定义并使用 更多的模板,它们本身必须被搜索和替换才能构建它们。我们会对 Vector 模板进行搜索和替换,等等。

“typedef”仅表示“当我说 Rect2 时,我的意思是 Rect”。同样,在 C# 中,您可以说

using MyStringList = System.Collections.Generic.List<string>;

C# 泛型和 C++ 模板之间存在许多差异。这里明显的区别是,C# 泛型只能使用类型进行参数化,而不能像此模板中那样使用进行参数化。另一个区别是 C++ 模板是在编译时完全构造的,并且只需使用程序中给出的实际参数即可合法构造。而 C# 泛型必须可以使用满足规定约束的任何参数来构造,并且新的代码生成直到运行时才会发生。

Based on your comments to the other answer, the problem you're facing is that you do not understand what C++ templates are. They are actually quite different from C# generics. They are conceptually similar, and syntactically similar, but their actual implementation is very different.

The easiest way to think about a template in C++ is as a "search and replace" mechanism. When you see

typedef Rect<double, 2> Rect2;

That means go find the template class for Rect that takes two template arguments. Here it is:

template<class Real, int Dim>
class Rect {

OK, so "double" corresponds to "class Real" and "2" corresponds to "int Dim". Sure enough, double is a type and 2 is an int, so that works properly. Now go through the template class and replace all instances of "Real" with "double" and "Dim" with 2. The result is:

typedef /* now we start replacing double for Real and 2 for Dim */ 
class Rect {
public:
typedef Vector<double, double> Vec;
typedef Rect<double, 2> Self;
typedef _RectPrivate::RectOp<2> RO;
Rect() : empty(true) {}
Rect(const Vec &vec) : empty(false), lo(vec), hi(vec) {}
...
} Rect2;

Notice that the result of search-and-replace on the template itself both defines and uses more templates, which have to themselves be search-and-replaced to construct them. We'd do a search-and-replace on the Vector template, and so on.

The "typedef" just means "when I say Rect2 I mean Rect<double, 2>". The same way in C# you can say

using MyStringList = System.Collections.Generic.List<string>;

There are many differences between C# generics and C++ templates. The obvious difference here is that C# generics can only be parameterized with types, never with values as is done in this template. Another difference is that C++ templates are fully constructed at compile time, and need only be legally constructable with the actual arguments given in the program. Whereas C# generics must be constructable with any arguments that meet the stated constraints, and the new code generation does not happen until runtime.

栀梦 2024-12-16 13:52:10

它为(别名)Rect 创建一个替代名称。
Rect 是一个类模板,有点类似于 C# 中的泛型。
因此,Rect2 是带有 Real=doubleDim=2Rect

it creates an alternative name for (alias) Rect<double, 2>.
Rect is a class template, which is somewhat comparable to Generics in C#.
So Rect2 is Rect with Real=double and Dim=2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文