Templates C++\CLI如何使用许多其他类可以访问的方法创建utils类

Templates C++\CLI如何使用许多其他类可以访问的方法创建utils类,templates,generics,c++-cli,Templates,Generics,C++ Cli,我有许多类使用相同的泛型函数/方法。目前,我已经为每个类编写了这些方法,但这涉及到不必要的重复。所以我想将这些方法移动到一个Utils类,所有需要使用这些方法的类都可以访问该类。我认为这可以通过通用或模板实现,但我没有一个我能理解的例子 这就是我现在所做的(忽略所有非必要的内容): 基因组h: ref class Genome { public: List<wchar_t>^ stringToList(String^ inString); // convert string

我有许多类使用相同的泛型函数/方法。目前,我已经为每个类编写了这些方法,但这涉及到不必要的重复。所以我想将这些方法移动到一个Utils类,所有需要使用这些方法的类都可以访问该类。我认为这可以通过
通用
模板
实现,但我没有一个我能理解的例子

这就是我现在所做的(忽略所有非必要的内容):

基因组h:

ref class Genome
{
public:
    List<wchar_t>^ stringToList(String^ inString);  // convert string to List with  chars
};
其中cString的类型为
List^

因此,如果我将
stringToList
方法移动到
Utils.h
Utils.cpp
类,代码会是什么样子,以及如何在classes Genome.cpp和其他类中调用此方法和其他Utils方法


谢谢,Jan

我把我的实用函数放在一个我标记为抽象密封的类中。因为类是抽象密封的,所以不能创建它的实例,因此所有成员都必须声明为静态的。结果相当于具有自由函数

例如,在标题中,您将看到

public ref class MyUtils abstract sealed
{

    // ........................................................................

    private:

        static MyUtils ();

    public:

        static System::Double CalculateAverage  ( cli::array<System::Double>^ arrayIn );



};
public ref类MyUtils抽象密封
{
// ........................................................................
私人:
静态MyUtils();
公众:
静态系统::双计算范围(cli::数组^arrayIn);
};
在.cpp中

MyUtils::MyUtils ()
{
}


System::Double MyUtils::CalculateAverage ( cli::array<System::Double>^ arrayIn )
{
    // the code doing the calculation...
}
MyUtils::MyUtils()
{
}
System::Double MyUtils::CalculateAverage(cli::array^arrayIn)
{
//正在进行计算的代码。。。
}
您可以像调用任何静态方法一样调用此方法

e、 g

cli::array^values=gcnew cli::array(5);
// ...
//在数组中放入一些数据
// ...
System::Double average=MyUtils::CalculateAverage(值);

当然,您也可以将泛型方法作为这个类的成员。

看看关于泛型的内容。在它的开头,有一个链接指向另一个解释泛型和模板之间差异的链接。关于使Utils::StringToList()方法在任何地方都可访问,只需将其标记为静态。@Jairo:静态确实有效!这基本上解决了我目前的问题。我确实看到了那篇文章,但我会对它进行更多的研究。谢谢,简
public ref class MyUtils abstract sealed
{

    // ........................................................................

    private:

        static MyUtils ();

    public:

        static System::Double CalculateAverage  ( cli::array<System::Double>^ arrayIn );



};
MyUtils::MyUtils ()
{
}


System::Double MyUtils::CalculateAverage ( cli::array<System::Double>^ arrayIn )
{
    // the code doing the calculation...
}
cli::array<System::Double>^ values = gcnew cli::array<System::Double>(5);
    // ...
    // put some data in the array
    // ...
System::Double average = MyUtils::CalculateAverage ( values );