MC++ FAQ
Saturday 08 | 21 | 2004
Managed Methods with Variable Argument Lists
In managed code, methods taking a variable argument list are not specified in the same way they are for unmanaged code (i.e. with "..."). Instead, what is supported is methods that take an __gc array as the last argument. To get these methods to be recognized as taking a variable argument list (as opposed to a simple method taking an array), the array parameter needs to be marked with the special [ParamsArray] attribute, like this:
String* MyOwnFormat(String* format, [ParamArray] Object* args[])

A method like this would be recognized in C# as taking a variable argument list. It's interesting to note that an argument marked with ParamArrayAttribute does not have to be of System::Object*[] type, but rather can be a managed array of any type, such as System::String*[], which could be interpreted as saying the method takes a variable number of arguments, all of type System::String*.

Unfortunately, the MC++ compiler, unlike C#, does not provide any simplified syntax for calling methods with ParamArray arguments. The only way of invoking them is to create the array yourself and pass that to the method:


Object* args[] = { S"asdasd", __box(12), S"ASD", __box(24) };
String* result = MyOwnFormat(S"{0}-{1}-{2}-{3}", args);

Visual C++ 2005 (aka Whidbey) will support variable argument lists in managed methods with the new Managed C++ Syntax (C++/CLI).

Posted by tomasr at August 21, 2004 08:46 AM
Copyright(C) 2002, Tomas Restrepo, All rights reserved. Powered by Movable Type 2.63