can a container contain references?

J

Jess

Hello,

Is it possible to put object references into a container, such as a
vector? I tried the following, but the compiler gave me lots of error
messages.

struct A{
int x;
};

int main(){
vector<A&> X;
A a;
X.push_back(a);
}

Thanks.
Jess
 
M

modemer

It's impossible as STL's implementation doesn't support it. If you
like this feature, you have to define your own container.

Cheers
 
G

Gianni Mariani

Jess said:
Hello,

Is it possible to put object references into a container, such as a
vector? I tried the following, but the compiler gave me lots of error
messages.

struct A{
int x;
};

int main(){
vector<A&> X;
A a;
X.push_back(a);
}


You can make a vector of pointers:

struct A{
int x;
};

int main(){
vector<A*> X;
A a;
X.push_back(&a);
}
 
F

Fei Liu

Jess said:
Hello,

Is it possible to put object references into a container, such as a
vector? I tried the following, but the compiler gave me lots of error
messages.

struct A{
int x;
};

int main(){
vector<A&> X;
A a;
X.push_back(a);
}

Thanks.
Jess

Container element must be copyable and assignable...If you check
push_back's prototype, then it's clear why you are getting all the error
messages.

void push_back(const T& x);

Fei
 
R

red floyd

Jess said:
Hello,

Is it possible to put object references into a container, such as a
vector? I tried the following, but the compiler gave me lots of error
messages.
No. Container elements must be assignable (and/or copyable?).
 
P

Pete Becker

Jess said:
Is it possible to put object references into a container, such as a
vector?

Not directly. But if you've got an implementation of TR1 (or Boost) you
can use a container of std::tr1::reference_wrapper<T> objects. A
reference_wrapper<T> acts pretty much like a T&, except that it can be
copied and assigned.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
B

blangela

No. Container elements must be assignable (and/or copyable?).

Once a reference has been initialized, it cannot be made to reference
any other object (unlike some other programming languages). In other
words, in the lifetime of a reference, it can reference one and only
one object.

Since the element of a container is normally constructed before an
object "is put in the container element", it is no longer possible to
store the object in the reference since the reference has already been
created and initialized. If it were possible to change the object
that a reference references, we would not have this problem. Since
pointers are allowed to point to different objects in their lifetime,
we must use pointers instead.

Are my statements above basically correct (sorry for the English
syntax, I did not start leaning English until I was 4 years old :) )?

Bob
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top