为什么C ++ CLI在托管类型上没有默认参数? [英] Why C++ CLI has no default argument on managed types?

查看:899
本文介绍了为什么C ++ CLI在托管类型上没有默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下行有错误默认参数不允许

public ref class SPlayerObj{
private:

    void k(int s = 0){ //ERROR
    }
}

为什么C ++在托管类型上没有默认参数?

我想知道是否有办法修复这个问题。

Why C++ has no default argument on managed types ?
I would like to know if there is a way to fix this.

推荐答案

它有可选参数,它们看起来和C ++语法看起来不一样。可选参数是一种语言互操作问题。它必须由进行调用的语言实现,它生成实际使用默认参数的代码。这是一个棘手的问题,在一个语言,旨在使交互轻松,如C / C / CLI,你当然不知道什么语言将打电话。或者如果它甚至有可选参数的语法。 C#语言直到版本4为止。

It does have optional arguments, they just don't look the same way as the C++ syntax. Optional arguments are a language interop problem. It must be implemented by the language that makes the call, it generates the code that actually uses the default argument. Which is a tricky problem in a language that was designed to make interop easy, like C++/CLI, you of course don't know what language is going to make the call. Or if it even has syntax for optional arguments. The C# language didn't until version 4 for example.

如果语言支持它,那么编译器如何知道默认值是什么。值得注意的是,VB.NET和C#v4选择了不同的策略,VB.NET使用了一个属性,C#使用了一个modopt。

And if the language does support it, how that compiler knows what the default value is. Notable is that VB.NET and C# v4 chose different strategies, VB.NET uses an attribute, C# uses a modopt.

你可以使用C ++中的[DefaultParameterValue] / CLI。但您不应该,结果无法预测。

You can use the [DefaultParameterValue] attribute in C++/CLI. But you shouldn't, the outcome is not predictable.

这篇关于为什么C ++ CLI在托管类型上没有默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆