【发布时间】:2017-05-11 07:05:46
【问题描述】:
我在 MSDN 上阅读了 c++ 的 foreach 语法:
// for_each_string1.cpp
// compile with: /ZW
#include <stdio.h>
using namespace Platform;
ref struct MyClass {
property String^ MyStringProperty;
};
int main() {
String^ MyString = ref new String("abcd");
for each ( char c in MyString )
wprintf("%c", c);
wprintf("/n");
MyClass^ x = ref new MyClass();
x->MyStringProperty = "Testing";
for each( char c in x->MyStringProperty )
wprintf("%c", c);
}
我试图在谷歌上找到“^”的含义,但我找不到任何东西(或者我的查询不正确)
这是什么意思?是“*”吗?是“&”吗?
此语法也适用于 C#。它们在两种语言中的意思是一样的吗?
一段 C# code:
using namespace System;
int main(){
array<int>^ arr = gcnew array<int>{0,1,2,5,7,8,11};
int even=0, odd=0;
for each (int i in arr) {
if (i%2 == 0)
even++;
else
odd++;
}
Console::WriteLine(“Found {0} Odd Numbers, and {1} Even Numbers.”,
odd, even);
}
【问题讨论】:
-
这不是 C++,而是微软的专有语言 C++/CLI。
-
这不是 C++,而是 C++/CLI。
-
DirectX 也使用这个。这到底是什么意思?
-
删除了 C# 标签,因为这个问题中没有 C#。
-
你的两个 sn-ps 都在 C++/CLI 中
标签: pointers reference c++-cli dereference