tags:

views:

152

answers:

7
  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.

+9  A: 
  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 explicitly create a vector of pointers and then store a pointer to your object.

sepp2k
+2  A: 

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.

ron
A: 
  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.

aaa
You can't "wrap" references.
anon
@Neil http://www.boost.org/doc/libs/1_42_0/doc/html/ref.html
aaa
@unknown That doesn't address storing references in collections. To quote: "The Ref library is a small library that is useful for passing references to function templates (algorithms) that would usually take copies of their arguments."
anon
@Neil one can make collection<ref<T> > and then references in collection become more or less transparent.
aaa
@unknown References are not copyable or assignable. The standard library containers require the things they contain to be both. No amount of wrapping can get round this.
anon
@Neil towards the end of link there is an example of using cref together with std:: vector http://www.boost.org/doc/libs/1_41_0/libs/multi_index/doc/tutorial/indices.html
aaa
@unknown Say what? That is a link to a completely different bit of boost! And no matter what it says, you cannot store references in a standard library container. Boost does not have some magical powers to enable it to do this!
anon
@Neil I never said reference can be stored in STL container. I said reference wrapper can be stored in STL container. Boost.Ref does not have container example, other pieces of boost do have example with vector and list (in spirit).
aaa
@unknown A wrapper means some code around an existing type - for example, a smart pointer is a wrapper around a raw pointer. But at the end of the day, one must be able to do the the operation on the wrapped type, and you simply cannot store references in standard library containers, no matter how you wrap them. That is part of the nature of the C++ language. I would observe that whenever I am wrong I freely admit it - perhaps you could benefit from my example?
anon
@Neil I did not invent naming convention, boost calls it reference. `std::vector<boost:: reference_wrapper<int> > v; v.push_back(ref(value)); ` is valid C++. I am not arguing that raw reference can be stored, I do insist that reference_wrapper provides feasible workaround. If you tell me where I am wrong, I will admit so.
aaa
@unknown No, it calls it reference_wrapper - I can call a class by any name I like, but that does not make it behave so. For example: typedef char * VerySmartPointer; does not actually create a very smart pointer.
anon
@Neil sorry, I meant wrapper in the first sentence
aaa
@unknown And I should have emphasised that you wilfully misunderstand the semantics of the boost type you are talking about. They are NOT references - take a look at the source code..
anon
@Neil I know they are not reference, I referred to type as wrapper, except for one place where I made mistake
aaa
@unknown I give up - congratulations.
anon
A: 

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.

Jeff Wilhite
A: 

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
John Knoeller
The word 'reference' is actually a very well-defined term in C++.
anon
@Neil: meaning that calling it 'jargon' is incorrect?
John Knoeller
@Neil: see http://en.wikipedia.org/wiki/Jargon: "A standard term may be given a more precise or unique usage among practitioners of a field"
John Knoeller
@John I would say so. At the least, it is as much jargon as "pointer".
anon
John Knoeller
@John Actually, as someone who has been using C since 1983, I never heard people talking about "pass by reference" - they all talked about "passing a pointer". Of course, what they were doing *was* pass by reference, but they didn't talk about it in that way.
anon
John Knoeller
Back in 1986 my mentor and I were also FORTRAN programmers, perhaps that is the difference. In FORTRAN 66 all function arguments are passed reference. (even constants)
John Knoeller
@John The first computer language I learned was FORTRAN IV, in 1970. As everything was pass by ref (as you observe) the term was never used.
anon
@John It has just struck me that this is getting to be a "well, I programmed a Babbage analytic engine in the days when they had to be hand-stoked with coal dug from t' central reservation of t' motorway" sort of thread. So no more on this from me :-)
anon
A: 

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.
Peter Alexander
Maybe you should also mention that C++ references and C#/Java references are totally different things.
sellibitze
Well, Java doesn't have references, but C# references aren't really that different other than the fact that there's no const references in C# and the syntax is different.
Peter Alexander
You do realize that in Java (as well as in C#) class-type variables are called references because they store null pointers or some other objects' addresses (thus *referring* to other objects), right?
sellibitze
A: 
  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.

David Thornley