开发者

General Array/Pointer Data Type

I'm cod开发者_如何学Going a C++ function that accepts a string, an array and the size of the array. It looks like this:

bool funcname (string skey, string sArr[], int arrSz)

I want to pass several array data types, such as double, char, long int, etc. Is it right to use string as data type for the array? Or is there a general data type that I can use?

Thanks in advance.


Using a string in this way is bad imo. Amongst other things you are sending an array of strings. You'd be better off using a std::vector. You could then template the function as follows:

template< typename T >
bool 
funcname (const std::string& skey, const std::vector< T >& arr )

This way you can directly query the vector for the size and you can pass through a vector of ANY data types.

Also bear in mind that its far more efficient to send structures through as const references instead of having a "potential" copy of the structures.


if you want arrays per se

template <typename T>
bool funcname (T key, T Arr[], int arrSz)
{
//T is the type of array you'll pass
}

also google for function templates. HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜