C + + CLI:cliext :: vector< T>作为公共类函数的返回类型? [英] C++ CLI: cliext::vector<T> as return type of public class function?

查看:1392
本文介绍了C + + CLI:cliext :: vector< T>作为公共类函数的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个类在C + + CLI:

  public ref class MyClass 
{
public:
MyClass(void);
virtual bool Init();

cliext :: vector< int> ^ ListOfNumbers();
};

我想从public fucntion中提取int的向量。



这里是impl:

  cliext :: vector< int> ^ MyClass :: ListOfNumbers $ b {
cliext :: vector< int> ^ devs = gcnew cliext :: vector< int>()
devs-> push_back(1);
return devs;
}

我的问题是我收到下一个警告:


警告C4677:'ListOfNumbers':非私人成员的签名
包含程序集私有类型'cliext :: vector< _Value_t>'


有人能告诉我原因吗?我可以从公共函数C ++ CLI类收到项目的集合吗?

解决方案

其实,我不知道,如果所有CLI 模板实例化是私有的,并且不能通过公共汇编接口暴露,则不会被惊讶。你知道,C ++ / CLI生成的模板类型在与其他.Net程序集交谈时并不真正有用,与其他.Net交谈只是C ++ / CLI的用途。 p>

Native C ++ templatesif(我可以这样调用)只能从C ++端使用和访问 only 。 CLR运行时不能对它们进行操作,因为编译器的C ++部分不能为它们生成适当的类型描述。 (*)



要创建.Net接口,请使用.Net BCL类型。使用泛型而不是模板。所以,看看类似 System.Collections.Generic.List<> 。他们可以安全地暴露在公共接口。 (**)



请记住,C ++ / CLI是本机C ++和.Net的混合。你很容易访问这两个世界,但这些世界并不真的喜欢混合;)



编辑:
(*)正如JochenKalmbach提醒,到本地模板。 Microsoft已经准备了一个实现一些核心集合接口的特殊STL版本,因此它的向量实际上实现了 System.Collection .Generic.IEnumerable 这里的仍然受限于本地限制,不能通过asembly接口直接发布(如 vector<> )。但是,后一种类型( IEnumerable )是完全正常的CLR类型,可以公开。所以,尝试例如:

  System :: Collections ::: Generic :: ICollection< int> ^ MyClass :: ListOfNumbers )
{
cliext :: vector< int> ^ devs = gcnew cliext :: vector< int>();
devs-> push_back(1)
return devs;
}

它可能会编译 - 但我没有检查。

I have next class in C++ CLI:

   public ref class MyClass
   {
        public:
           MyClass(void);
           virtual bool Init();

           cliext::vector<int>^ ListOfNumbers();
   };

I would like to recive vector of int from public fucntion.

Here is impl:

cliext::vector<int>^ MyClass::ListOfNumbers()
{
    cliext::vector<int>^ devs = gcnew cliext::vector<int>();
    devs->push_back(1);
    return devs;
}

My problem is that I got next warning:

warning C4677: 'ListOfNumbers': signature of non-private member contains assembly private type 'cliext::vector<_Value_t>'

Could someone tell me the reason? Could I receive collection of items from public function C++ CLI class?

解决方案

Actually, I don't know it, but I wouldn't be suprised if all CLI template instantiations were private and not exposable through the public assembly interface. You know, the template types generated by C++/CLI aren't really helpful when talking to other .Net assemblies, and "talking to other .Net" is just what C++/CLI is for.

"Native C++ templates" if (I can call them like that) are usable and accessible only from C++ side. CLR runtime cannot operate on them in general, as the C++ part of the compiler is not able to generate proper type description for these. (*)

For creating .Net interfaces, use .Net BCL types. Use generics instead of templates. So, look at types like System.Collections.Generic.List<>. They can safely be exposed in public interfaces. (**)

Remember that C++/CLI is a mixture of native C++ and .Net. You get easy access to both worlds, but those worlds doesn't really like being mixed ;)

EDIT: (*) As JochenKalmbach reminded, it applies to the "native" templates. Microsoft has prepared a special STL version that implements some of the core collection interfaces, hence its vector actually implements System.Collection.Generic.IEnumerable. That vector here is still subject to native restriction and cannot be published through asembly interface directly as itself (as vector<>). However, the latter type (IEnumerable) is completely normal CLR type and can be exposed. So, try for example:

System::Collections:::Generic::ICollection<int>^ MyClass::ListOfNumbers()
{
    cliext::vector<int>^ devs = gcnew cliext::vector<int>();
    devs->push_back(1);
    return devs;
}

It probably will compile - but I've not checked.

这篇关于C + + CLI:cliext :: vector&lt; T&gt;作为公共类函数的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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