开发者

C++ Array and Vector are hold values or references?

  1. In C++ we have value type (int, long, float, ...) and reference type (class, struct, ...).

  2. For value type, Array and Vector hold the actual values;

  3. For reference type, Array and Vector开发者_运维问答 only hold the references to these objects;

  4. So when we put reference type into Array and Vector, we need to make sure those objects will exist long enough (valid during the entire process) to avoid exception/error;

My above statements are correct or not? please correct me if I am wrong.


  1. No. Any type can be passed by value or by reference (also any type can be created on the stack or on the heap, though you didn't ask that).

  2. For any type Arrays and Vectors hold the actual values. Because of this any type stored in a vector needs to be copy-constructible.

  3. See 2.

  4. Nope. That's only the case if you explicitly create a vector of pointers and then store a pointer to your object.


Classes are value types from your perspective also. If you put the instance 'a' of class Apple into a vector<Apple>, the so called copy constructor will be used to create a copy of 'a' which will be put into the vector.


  1. I think you meant plain datatype and derived datatype. Value/reference refers to passing by value/by reference most of the time

  2. Array/vector/collection always holds values

  3. Reference cannot be stored in array/collection directly, you have to wrap reference.

  4. I think you meant pointers.


Remember that if you fill your vector with pointers to objects, you are responsible for deleting those objects yourself. If the vector is the only place those pointers are kept, you will leak memory when the vector is destroyed unless you call delete on each pointer in the vector.

If you fill your vector with actual objects (not pointers), a copy of the object is made when it is inserted into the vector. In this case, you do not have to worry about deleting the objects yourself; they are automatically deleted when the vector is destroyed.


Yes, but with two caveats. The word 'reference' is a jargon term for C++, I think you mean 'pointer'. You can't put references (&) in an array, but you can put pointers (*) into an array.

second it is possible to create an array that contains structs/classes rather than pointing to them.

struct _foo {
  ...
  }; 

struct _foo ary[3]; // an array of 3 foos, the foos are _in_ the array, no rule 4

struct * _foo ary[3]; // an array of 3 pointers to foo, rule 4 applies


You appear to be from a Java or C# background. In C++, everything is by default a value type, and is only a reference type if you specifically specify. All standard containers and most library containers store values by default also.

Examples to demonstrate value types and reference types:

int a; // a is of a value type
int& b = a; // b is of a reference type, modifying b will modify a.

MyClass c; // c is of a value type
MyClass& d = c; // d is of a reference type, modifying d will modify c.


  1. We have types in C++, not value and reference types. Values of any type can be allocated directly on the stack or the heap, and may wind up being referred to either by pointer or reference.

  2. Arrays and vectors contain data objects. These can be the objects you're interested in, or pointers of some sort to them. (In C++, a pointer to a type is itself a type.)

  3. See 2.

  4. That's what smart pointers are for. A Boost shared_ptr<> will delete what it points to when it's no longer accessible, being a small-scale reference-counted garbage collector.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜