数组型参数和数组的区别

// templatetest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <typeinfo>
template <class Type, int size>

Type min( const Type (&r_array)[size] )
{
    /* 找到数组中元素最小值的参数化函数 */
    Type min_val = r_array[0];

    for ( int i = 1; i < size; ++i )
        if ( r_array[i] < min_val )
            min_val = r_array[i];

    return min_val;
}

template <class Type, int size>
Type min( Type (&r_array)[size] ) { /* ... */ return size; }

void f( double pval[9] ) {
    // 错误: Type (&)[] != int*
    std::cout<<typeid(pval).name()<<std::endl;// output double *
}

int _tmain(int argc, _TCHAR* argv[])
{
    double da[8] = { 10.3, 7.2, 14.0, 3.8, 25.7, 6.4, 5.5, 16.8 };
    int i1 = min( da );
    f(da);
    std::cout<<*da<<std::endl;
    std::cout<<typeid(da).name()<<std::endl;//output double [8]
    getchar();
    return 0;
}

posted on 2010-08-19 13:15  ATAK  阅读(250)  评论(0编辑  收藏  举报

导航