Vector of object references

H

hardwareman

Hi All,

I've read that I should use references instead of pointers, and as I do
have some suspicious casts floating around in my code, I thought it a
good idea to remove them. Ultimate datastructure is a map of Vectors
of references ((sb_iter->second)[0])->GetPktId(),

I wish to have a vector of object references instead of of a vector of
pointers to the objects e.g.:

typedef vector<PKT &> PKTchain_T ; <-- doesn't compile
typedef vector<PKT *> PKTchain_T ; <-- works fine

TESTCASE yields many errors like:
/usr/include/c++/3.2.2/bits/stl_vector.h:115: forming pointer to
reference type

Is this a case where I should stick with the pointers?

--- TESTCASE START ---
#include <map>
#include <vector>
#include <stdio.h>
using namespace std ;
typedef vector<int &> VINT_T ;
VINT_T vint(20) ;
int main () {
return 0 ;
}
--- TESTCASE END ---
removing the & in the typedef compiles fine :
typedef vector<int > VINT_T ;
 
W

Wiseguy

hardwareman said:
Hi All,

typedef vector<PKT &> PKTchain_T ; <-- doesn't compile
typedef vector<PKT *> PKTchain_T ; <-- works fine

IIRC, vectors MUST be initialized to point to real objects and since
you can have empty elements in a vector you could end up with
elements of the vector with uninitialized references.

I'd stick with pointers but I'm curious whether someone else has a
different take on this.
 
S

Stephen Howe

Is this a case where I should stick with the pointers?

Yes. Or smart pointers.

For vectors, whatever you put in a vector has to be copy constructable and
assignable. A requirement.
A referemce is _NOT_ copy constructable.

Stephen Howe
 
J

Jeff Flinn

hardwareman said:
Hi All,

I've read that I should use references instead of pointers, and as I
do have some suspicious casts floating around in my code, I thought
it a good idea to remove them. Ultimate datastructure is a map of
Vectors of references ((sb_iter->second)[0])->GetPktId(),

I wish to have a vector of object references instead of of a vector of
pointers to the objects e.g.:

You haven't provided info about what PKT really is, or how you intend to use
the vector. Are you aware that you may store instances of builtin types and
classes directly in a vector? This assumes the class is copyable/assignable.
typedef vector<PKT &> PKTchain_T ; <-- doesn't compile
typedef vector<PKT *> PKTchain_T ; <-- works fine

TESTCASE yields many errors like:
/usr/include/c++/3.2.2/bits/stl_vector.h:115: forming pointer to
reference type

Is this a case where I should stick with the pointers?

You don't provide enough info to decide yet.

Jeff Flinn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top