类函数是C++/CLI中引入的新概念,其功能类似于函数模板,但原理上却迥然不同。使用函数模板时,编译器根据模板生成函数源代码,然后将其与其它代码一起编译。这种方法可能会产生许多函数,从而使得生成模块的体积增加,这就是所谓的“代码膨胀”。类函数与之不同,类函数本身将被编译,在调用类函数时,实际类型在运行时取代了类函数的类型形参,这不会导致新增代码的问题。

一、类函数的定义

类函数与普通函数的区别在于:它需要定义一个特殊的形参——类型形参,该参数说明在运行时传递给函数的参数类型。下面的例子定义了一个类函数,用于找出某种数据类型数组中最大的元素。

generic<typename T> where T:IComparable
T MaxElement(array<T>^ x)
{
	T max = x[0];
	for(int i=1; i<x->Lenght; i++)
		if(max->CompareTo(x[i])<0)
			max = x[i];
	return max;
}

关键字generic规定后面的内容为类函数定义,尖括号内用关键字typename定义了一个类型形参T,如果有多个类型形参,它们都放在尖括号内,用逗号分开。

关键字where引入使用类函数时,传递给T的类型实参应满足的约束条件。这里的条件是:所有用于替代T的类型必须实现了IComparable接口。该约束也意味着,所有传入的类型实参都实现了CompareTo()函数,允许对该类型两个对象进行比较。

第二行定义了函数的返回值类型、函数名称以及形参,与普通函数定义类似,但有的类型用T来描述,它们将在运行时被确定。

二、使用类函数

调用类函数的方法与调用普通函数的方法一样。对上面声明的MaxElement()函数可如此调用:

array<double>^ data = {1.5, 3.5, 6.7, 4.2, 2.1};
double maxData = MaxElement(data);

 在这个例子中,编译器能够判断出该类函数的类型实参为double,生成调用函数的代码(并非是该类函数的double版),执行时再用 double代替T。注意:与函数模板不同,对于类函数编译器不创建函数实例,而是仅仅是使编译后的代码可以接受数据类型作为类型实参,实际的类型替换在运行时实现。

应该注意的是,如果以字符串常量作为实参传递给类函数,编译器将认为类型实参是String^,无论该字符串常量是窄字符串(“Hello”)还是宽字符串(L“Hello”)。

有些情况下,编译器无法根据函数调用来判断类型实参是什么,这时可用在函数名后面加尖括号和类型名称来显示的指定,上面的例子也可以写作

double maxData = MaxElement<double>(data);

另外需要注意的是,提供给类函数作为类型实参的不能是本地C++类型、本地指针、引用,也不能是值类类型的句柄(如 int^)。而只能是值类型(如int、double)或引用类型的跟踪句柄(如String^)。

下面是一个使用类函数的完整示例。

- - - - - - - - - - - - - - - - <<== 华丽的分割线 ::开始==>> [Ex6_10.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex6_10.cpp : main project file.
// Defining and using generic fuctions
#include "stdafx.h"

using namespace System;

// Generic function to find the maximum element in an array
generic<typename T> where T:IComparable
T MaxElement(array<T>^ x)
{
	T max = x[0];
	for(int i=1; i<x->Length; i++)
		if(max->CompareTo(x[i])<0)
			max = x[i];
	return max;
}

// Generic function to remove an element from an array
generic<typename T> where T:IComparable
array<T>^ RemoveElement(T element, array<T>^ data)
{
	array<T>^ newData = gcnew array<T>(data->Length-1);
	int Index = 0;		// Index to elements in newData array
	bool found = false;	// Indicates that the element to remove from was found
	for each(T item in data)
	{
		// Check for invalid index or element found
		if((!found) && item->CompareTo(element)==0 )
		{ 
			found = true;
			continue;
		}
		else
		{
			if(Index == newData->Length)
			{
				Console::WriteLine(L"Element to remove not found");
				return data;
			}
			newData[Index++] = item;
		}
	}
	return newData;
}

// Generic function to list an array
generic<typename T> where T:IComparable
void ListElements(array<T>^ data)
{
	for each(T item in data)
		Console::Write(L"{0, 10}", item);
	Console::WriteLine();
}

int main(array<System::String ^> ^args)
{
	array<double>^ data = {1.5, 3.5, 6.7, 4.2, 2.1};
	Console::WriteLine(L"Array contains:");
	ListElements(data);
	Console::WriteLine(L"\nMaximun element = {0}\n", MaxElement(data));
	array<double>^ result = RemoveElement(MaxElement(data), data);
	Console::WriteLine(L"After removing maximun, array contains:");
	ListElements(result);


	array<int>^ numbers = {3, 12, 7, 0, 10, 11};
	Console::WriteLine(L"Array contains:");
	ListElements(numbers);
	Console::WriteLine(L"\nMaximun element = {0}\n", MaxElement(numbers));
	Console::WriteLine(L"After removing maximun, array contains:");
	ListElements(RemoveElement(MaxElement(numbers), numbers));

	array<String^>^ strings = {L"Many", L"hands", L"make", L"light", L"work"};
	Console::WriteLine(L"Array contains:");
	ListElements(strings);
	Console::WriteLine(L"\nMaximun element = {0}\n", MaxElement(strings));
	Console::WriteLine(L"After removing maximun, array contains:");
	ListElements(RemoveElement(MaxElement(strings), strings));

	return 0;
}
- - - - - - - - - - - - - - - - <<== 华丽的分割线 ::结束==>> [Ex6_10.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

输出如下

Array contains:
       1.5       3.5       6.7       4.2       2.1

Maximun element = 6.7

After removing maximun, array contains:
       1.5       3.5       4.2       2.1
Array contains:
         3        12         7         0        10        11

Maximun element = 12

After removing maximun, array contains:
         3         7         0        10        11
Array contains:
      Many     hands      make     light      work

Maximun element = work

After removing maximun, array contains:
      Many     hands      make     light