C ++ / CLI通用构造函数约束 [英] C++/CLI generic constructor constraint
问题描述
嗨
我正在尝试编写一个实例化通用
类型的泛型类,但我找不到正确的方法来给它构造函数
约束。
例如:
在C#中:
class X< T>其中T:new()
{
static public T F(){return new T(); }
}
在C ++ / CLI中:
generic< class T>哪里T:???? //(new(),gcnew()或什么?
class X
{
public:
static T ^ F(){return gcnew T();}
};
Alon Fliess写道:< blockquote class =post_quotes>嗨
我正在尝试编写一个实例化通用
类型的泛型类,但我找不到给它构造函数的正确方法
约束。
例如:
在C#中:
类X< T>其中T:new()
{
static public TF(){return new T();}
}
在C ++ / CLI中:
generic< class T>其中T:???? //(new (),gcnew()还是什么?
class X
{
公开:
静态T ^ F(){return gcnew T();}
};
IIUC,C ++ / CLI不支持为VC ++
2005版本创建通用约束。它将遵循通用约束generics用另一种语言(C#)撰写了
。据我所知它,ECMA委员会仍然在研究语法,它将支持泛型,但不会与已经很复杂的C ++类型说明符语法碰撞。
-cd
Alon,
IIUC,C ++ / CLI不支持创建通用约束
VC ++ 2005发布。然而,它将尊重用另一种语言(C#)创作的泛型的通用约束。据我了解,ECMA委员会仍在研究支持泛型的语法,但不会与已经复杂的C ++类型说明符语法相冲突。
只是为了补充Carl写的内容,你仍然可以在C ++ / CLI中执行此操作,但是你需要反射来创建泛型中的对象。这是一个
的例子:
ref class A
{
public:
A(){Console :: WriteLine(" In Constructor()"); }
};
generic< class T>
ref class B
{
public:
T CreateT(){
return(T)Activator :: CreateInstance(T :: typeid);
}
};
int main()
{
B< A ^> ^ b = gcnew B< A ^> ;;
A ^ a = b-> CreateT();
}
-
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/weblog
" Tomas Restrepo \(MVP \)" <到**** @ mvps.org>在消息新闻中写道:< us ************** @ TK2MSFTNGP14.phx.gbl> ...Alon,
IIUC,C ++ / CLI不支持为VC ++ 2005版本创作通用约束。然而,它将尊重用另一种语言(C#)创作的泛型的通用约束。据我了解,ECMA委员会仍在研究支持泛型的语法,但不会与已经复杂的C ++类型说明符语法相冲突。
为了补充Carl写的内容,您仍然可以在C ++ / CLI中执行此操作,但是您需要反射来创建泛型中的对象。这是一个
示例:
ref class A
{
public:
A(){Console :: WriteLine(" In Constructor) ()"); }
};
泛型< class T>
ref class B
{
公开:
T CreateT(){
return(T)Activator :: CreateInstance(T :: typeid);
}
};
int main()
{
B< ; A ^>> ^ b = gcnew B< A ^> ;;
A ^ a = b-> CreateT();
}
嗨
这很有趣,我想比较一个基于C ++的工厂模式和激活机制
,找出性能差异和
其他优点/缺点(类型安全等)我得到一个答案,我可以使用
激活器....
我我认为我将使用模板来创建工厂,但是其他语言必须实现他们的具体创建者
(泛型会解决的问题)
Alon。
Hi
I am trying to write a generic class that instantiates the generic
type, but I can not find the correct way to give it the constructor
constraint.
For example:
In C#:
class X<T> where T:new()
{
static public T F() { return new T(); }
}
In C++/CLI:
generic<class T> where T:???? //(new(), gcnew() or what?
class X
{
public:
static T ^F() { return gcnew T(); }
};
Alon Fliess wrote:Hi
I am trying to write a generic class that instantiates the generic
type, but I can not find the correct way to give it the constructor
constraint.
For example:
In C#:
class X<T> where T:new()
{
static public T F() { return new T(); }
}
In C++/CLI:
generic<class T> where T:???? //(new(), gcnew() or what?
class X
{
public:
static T ^F() { return gcnew T(); }
};
IIUC, C++/CLI doesn''t support authoring of generic constraints for the VC++
2005 release. It will honor generic constraints of generics authored in
another language (C#), however. As I understand it, the ECMA committee is
still working on syntax that will support generics but not collide with the
already complicated C++ type specifier syntax.
-cd
Alon,
IIUC, C++/CLI doesn''t support authoring of generic constraints for the
VC++ 2005 release. It will honor generic constraints of generics authored
in another language (C#), however. As I understand it, the ECMA committee
is still working on syntax that will support generics but not collide with
the already complicated C++ type specifier syntax.
Just to complement what Carl wrote, you can still do this in C++/CLI, but
you''ll need reflection to create the object inside your generic. Here''s an
example:
ref class A
{
public:
A() { Console::WriteLine("In Constructor()"); }
};
generic <class T>
ref class B
{
public:
T CreateT() {
return (T)Activator::CreateInstance(T::typeid);
}
};
int main()
{
B<A^>^ b = gcnew B<A^>;
A^ a = b->CreateT();
}
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/weblog
"Tomas Restrepo \(MVP\)" <to****@mvps.org> wrote in message news:<us**************@TK2MSFTNGP14.phx.gbl>...Alon,IIUC, C++/CLI doesn''t support authoring of generic constraints for the
VC++ 2005 release. It will honor generic constraints of generics authored
in another language (C#), however. As I understand it, the ECMA committee
is still working on syntax that will support generics but not collide with
the already complicated C++ type specifier syntax.
Just to complement what Carl wrote, you can still do this in C++/CLI, but
you''ll need reflection to create the object inside your generic. Here''s an
example:
ref class A
{
public:
A() { Console::WriteLine("In Constructor()"); }
};
generic <class T>
ref class B
{
public:
T CreateT() {
return (T)Activator::CreateInstance(T::typeid);
}
};
int main()
{
B<A^>^ b = gcnew B<A^>;
A^ a = b->CreateT();
}
Hi
This is funny, I wanted to compare a C++ based factory pattern with
the Activator mechanism, to find out the performance differences and
other pros/cons (type safety, etc.) And I get an answer that I can use
the activator....
I think that I will use a template to create the Factory, however
other languages will have to implement their concrete creator
(something that generic would have solved)
Alon.
这篇关于C ++ / CLI通用构造函数约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!