开发者

c++ return data in a value parameter?

I'm trying to understand the difference between

void getval(const int record, int32* val, bool allowExc) const;

and

void getval(const int record, vector<int32>* value, bool allowExc) const;

I need to return a list of开发者_JAVA技巧 int32s. Can the 1st example return a list of ints32s or only one? Any help is appreciated


Both functions take 3 arguments, return no value, and do not modify the object which they are associated with. The difference is in the second parameter.

The first function takes a pointer to an int32 (which is presumably, but not necessarily, a 32-bit signed integer). This might be a pointer to the first element of an array of int32 values, or a pointer to an individual value. The value(s) can be modified by the function.

The second function takes a pointer to a vector of int32. This is an array, and because it is a non-const parameter, the vector can be modified (with care) by the function. One of the many advantages of a vector over a plain pointer is that you can find out how many elements are in the vector, but there is no way for the function to tell how many elements are in the array associated with the pointer unless one of the other two parameters indicates the size.


Update to question:

I need to return a list of int32s. Can the 1st example return a list of ints32s or only one?

The first function can assign values to memory allocated by its caller, but cannot usefully allocate memory and pass that back to the caller. If the caller passes in a pointer to a list (array) of int32 values, then the function can overwrite that list; it does not (self-evidently) know how big that list is.

The second function gets a pointer to a vector of int32 values. The function could assign a new vector to the pointer, leaving open the question of how the previous value was released (or leaked). It could also simply modify the vector, but in that case, a reference would be easier to understand. The vector would take care of the memory management.


The difference is obviously between int32* and vector<int32>*. While the two are doing basically the same thing you should know that int32* is a primitive data type ( a pointer to an array of int32s) while vector is a STL (Standard Template Library) class holding in this case an array of int32s while offering more options like:

  • out of bounds checking ([] operator)
  • size of the vector (size function )
  • maximum capacity of the vector (capacity function)
  • methods for reserving more bytes if needed (reserve function)

You don't have any of these with the primitive data type.

All about vector template class you can find on its help page.


The first function has the val parameter as a single pointer to a int32, while the second has the value parameter as a pointer to a vector of int32. (a vector can store multiple variables of the same type)


vector is an STL container class. In your case, it is the difference between an int32 pointer and a pointer to a vector of int32s.

For more information, I suggest you read up on http://www.cplusplus.com/reference/stl/vector/.


Sometimes, parameters are used to return values. In this case, val is a pointer to an int32, so we could write into *val and leave a value there. For value, we could do something like value->push_back(an int32 value) or (*value)[0] = an int32 value.


just wondering, why not use the vector as a reference parameter (i.e., vector<int32>& value)?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜