
Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
using namespace System;
public ref class Stack {
public:
Stack() {
first = nullptr;
}
property bool Empty {
bool get() {
return (first == nullptr);
}
}
Object^ pop() {
if (first == nullptr)
throw gcnew Exception("Can't Pop from an empty Stack!");
else {
Object^ temp = first->Value;
first = first->Next;
return temp;
}
}
void Push(Object^ o) {
first = gcnew Node(o, first);
}
ref struct Node {
Node^ Next;
Object^ Value;
Node(Object^ value) {
Next = nullptr;
Value = value;
}
Node(Object^ value, Node^ next) {
Next = next;
Value = value;
}
};
private:
Node^ first;
};
int main()
{
Stack^ s = gcnew Stack();
for (int i = 0; i < 10; i++)
s->Push(i);
s = nullptr;
}
> I've included a corrected sample below. Now that a compiler is available,
> one of my tasks (somewhere around #50 on my to do list) is to extract all
> the examples from our language specification and compile them.
Thanks for the corrections. Since there is no real tutorial yet on C++/CLI,
except things like your webcast on the VC site which I enjoyed and other
small snippets, I figure I'm going to learn about it as much as I can by
just reading the specs and running the examples. Here's another example
that did not compile:
#using <mscorlib.dll>
using namespace System;
using namespace stdcli::language;
void F(... array<int>^ args) {
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine("\targs[{0}] = {1}", i, args[i]);
}
int main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(gcnew array<int> {1, 2, 3, 4});
}
This one is for the parametized array. I get these errors:
8_3.cpp
8_3.cpp(5) : error C2144: syntax error : 'stdcli::language::array<Type>'
should be preceded by ')'
with
[
Type=int
]
8_3.cpp(5) : warning C4518: '?$array@H$00 ' : storage-class or type
specifier(s) unexpected here; ignored
8_3.cpp(5) : error C2143: syntax error : missing ';' before '^'
8_3.cpp(5) : error C2059: syntax error : ')'
8_3.cpp(5) : error C2470: 'args' : looks like a function definition, but
there is no formal parameter list; skipping apparent body
- Don Kim
Brandon Bray [MSFT] - 21 Jul 2004 21:45 GMT
> Thanks for the corrections. Since there is no real tutorial yet on
> C++/CLI, except things like your webcast on the VC site which I enjoyed
> and other small snippets, I figure I'm going to learn about it as much as
> I can by just reading the specs and running the examples.
I'm working hard towards getting more content, as are a few other people. In
any case, I'm happy to assist in forums however I can.
> Here's another example that did not compile:
This example regarding param arrays also demonstrates some unfinished work
in the compiler. Instead of using the ... for the param array, substitute
the actual attribute that goes into metadata:
System::Runtime::InteropServices::ParamArray
That will make it work now. Later you will be able to use the ... instead.
I've copied a corrected sample that compiles.

Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
using namespace System;
using namespace stdcli::language;
using namespace System::Runtime::InteropServices;
void F([ParamArray] array<int>^ args) {
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine("\targs[{0}] = {1}", i, args[i]);
}
int main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(gcnew array<int> {1, 2, 3, 4});
}
Hi Brandon,
> I've included a corrected sample below. Now that a compiler is available,
> one of my tasks (somewhere around #50 on my to do list) is to extract all
> the examples from our language specification and compile them.
Need any help with that? ;)

Signature
Tomas Restrepo
tomasr@mvps.org
Brandon Bray [MSFT] - 21 Jul 2004 21:46 GMT
Tomas Restrepo (MVP) wrote:
> Need any help with that? ;)
Sure. I'll follow up offline. :-)

Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.