We've got a code sample today, of a simple integer stack implemented using Whidbey
C++. I'll implement it using three pieces of code:
- stack.h - definition of the stack
- stack.cpp - implementation of the stack
- main.cpp - driver program that makes use of the stack
I compile the stack class into a DLL (stack.dll), which I then import into the main
program with #using. The beauty of stack.dll is that it should be consumable
by any .NET-targeted language: C#, VB.NET, even ASP.NET. In fact, to prove this,
I've also included drv.cs, a C# source file which consumes stack.dll.
Another benefit of using DLLs: during development, I found a bug in my stack program.
I edited stack.cpp, recompiled the DLL, and ran main.exe again. I didn't need
to recompile main! 🙂
- stack.cpp as a DLL: cl /clr:newSyntax /LD stack.cpp
- main.cpp: cl /clr:newSyntax main.cpp
- drv.cs: csc drv.cs /r:stack.dll
Get the files! The code is available as a zip file: stack.zip.