C++ 如何通过引用传递泛型::列表?

C++ 如何通过引用传递泛型::列表?,c++,.net,pass-by-reference,argument-passing,C++,.net,Pass By Reference,Argument Passing,在尝试将一些非托管代码包装到托管.dll中时,我尝试将数据点的Generic::List转换为std::vector。下面是我尝试做的一个片段: namespace ManagedDLL { public ref class CppClass { void ListToStdVec( const List<double>& input_list, std::vector<double>& output_vector )

在尝试将一些非托管代码包装到托管.dll中时,我尝试将数据点的
Generic::List
转换为
std::vector
。下面是我尝试做的一个片段:

namespace ManagedDLL
{
    public ref class CppClass
    {
        void ListToStdVec( const List<double>& input_list, std::vector<double>& output_vector )
        {
            // Copy the contents of the input list into the vector
            // ...
        }

        void ProcessData( List<double> sampleData )
        {
            std::vector<double> myVec;

            ListToStdVec( sampleData, myVec );

            // Now call the unmanaged code with the new vector
            // ...
        }
    }
}
namespace ManagedDLL
{
公共参考类
{
无效列表TOSTDVEC(常量列表和输入列表,标准::向量和输出向量)
{
//将输入列表的内容复制到向量中
// ...
}
void ProcessData(列出sampleData)
{
std::载体myVec;
ListToStdVec(sampleData,myVec);
//现在使用新向量调用非托管代码
// ...
}
}
}
编辑这篇文章给了我:

错误C3699:“&”:无法对类型“const System::Collections::Generic::List”使用此间接寻址

我可能在这里遗漏了一些基本的东西(我对.net的工作方式相对来说比较陌生),但对我来说这似乎是合理有效的代码

[编辑]我尝试了Andy和Dario的建议,它们都有效,但我如何访问输入列表的成员?我尝试过各种数据引用的组合,但似乎没有编译:

void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector )
{
    int num_of_elements = input_list->Count;
}

void ListToStdVec( const List<double>^ input_list, std::vector<double>& output_vector )
{
    int num_of_elements = input_list.Count;
}
void ListToStdVec(常量列表%input\u List,std::vector&output\u vector)
{
int num_of_elements=输入_列表->计数;
}
void ListToStdVec(常量列表^input\u List,std::vector和output\u vector)
{
int num_of_elements=input_list.Count;
}
…两者都给了我:

错误C2662:'System::Collections::Generic::List::Count::get':无法将'this'指针从'const System::Collections::Generic::List'转换为'System::Collections::Generic::List%'

…那么如何访问引用/指针呢?

因为
列表
是一个托管的.NET类,它是通过由^表示的托管GC句柄传递的,而不是通过C++-reference传递的

例:

void ListToVec(列表^input\u列表,std::vector&out)
这里不需要额外的
const
。符号
List^%
创建跟踪引用(与C++指针相当),而不是按引用调用。 只需通过
列表->…
列表[…]
访问成员,根据,
%
是托管对象按引用传递字符。将代码转换为以下代码,它应该可以工作:

void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector
{
    // Copy the contents of the input list into the vector
    // ...
}
void ListToStdVec(常量列表%input\u List,std::vector&output\u vector
{
//将输入列表的内容复制到向量中
// ...
}
编辑:我认为是
常量
引起了问题,尽管我不确定原因。如果将
列表
参数更改为非
常量
,那么如果使用
->
运算符,第一个函数将编译,而如果使用
运算符,第二个函数将编译(我不确定为什么会存在这种差异——这没有多大意义)


也就是说,如果您只想将
列表中的元素
复制到
向量
,那么您真的想使用
^
。可以将其视为对托管对象的引用。如果您想“按引用”传递引用,则可以使用
%
(即将
input_list
重新分配给
ListToStdVec()中的其他内容)
,并让调用者查看该赋值的结果。但是,如果您在使用
%
时使用
操作符访问成员,这告诉我,我可能根本不理解该操作的目的。

如果是这样,为什么input\u list->Count对^operator有效,而input\u list.Count对%operator有效R?传统地表示相反的(从C++视角说话)?R.E.你的编辑;我得出相同的结论,我也同样被误解了:-删除const确实允许我访问输入列表的成员,但是我喜欢在任何可能的地方输入输入,因为它经常在编译时捕获错误。
void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector
{
    // Copy the contents of the input list into the vector
    // ...
}