[ACCEPTED]-Why C++ CLI has no default argument on managed types?-default-parameters
It does have optional arguments, they just 18 don't look the same way as the C++ syntax. Optional 17 arguments are a language interop problem. It 16 must be implemented by the language that 15 makes the call, it generates the code that 14 actually uses the default argument. Which 13 is a tricky problem in a language that was 12 designed to make interop easy, like C++/CLI, you 11 of course don't know what language is going 10 to make the call. Or if it even has syntax 9 for optional arguments. The C# language 8 didn't until version 4 for example.
And 7 if the language does support it, how that 6 compiler knows what the default value is. Notable 5 is that VB.NET and C# v4 chose different 4 strategies, VB.NET uses an attribute, C# uses 3 a modopt.
You can use the [DefaultParameterValue] attribute 2 in C++/CLI. But you shouldn't, the outcome is not 1 predictable.
In addition to the precise answer from Hans Passant, the answer 4 to the second part on how to fix this, you 3 are able to use multiple methods with the 2 same name to simulate the default argument 1 case.
public ref class SPlayerObj {
private:
void k(int s){ // Do something useful...
}
void k() { // Call the other with a default value
k(0);
}
}
An alternative solution is to use the [OptionalAttribute]
along 3 side a Nullable<int>
typed parameter. If the parameter 2 is not specified by the caller it will be 1 a nullptr
.
void k([OptionalAttribute]Nullable<int>^ s)
{
if(s == nullptr)
{
// s was not provided
}
else if(s->HasValue)
{
// s was provided and has a value
int theValue = s->Value;
}
}
// call with no parameter
k();
// call with a parameter value
k(100);
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.