473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

can a container contain references?

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

May 9 '07 #1
6 2010
It's impossible as STL's implementation doesn't support it. If you
like this feature, you have to define your own container.

Cheers

On May 9, 10:23 am, Jess <w...@hotmail.comwrote:
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

May 9 '07 #2
Jess wrote:
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);
}
May 9 '07 #3
Jess wrote:
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
May 9 '07 #4
Jess wrote:
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?).
May 9 '07 #5
Jess wrote:
>
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<Tobjects. A
reference_wrapper<Tacts 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)
May 9 '07 #6
On May 9, 8:11 am, red floyd <no.s...@here.dudewrote:
Jess wrote:
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?).
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

May 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared...
32
by: Severin Ecker | last post by:
hi! normally i would simply do the following: std::vector<Element> vec; void somefunc() { Element e; vec.push_back(e); }
5
by: Tony Johansson | last post by:
Hello Experts! I just wonder about this question. A polymorf container-object must always store references to those object that exist in the contain object. This statement must be false. ...
5
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
8
by: puzzlecracker | last post by:
Guys, what is more preferable, given that I don't have access to boost or shared pointers: std::map<T*, std::set<T2** *_myMap; vs. std::map<T*, std::set<T2* *myMap; Notice that in the...
5
by: Olumide | last post by:
Hello - To begin with, I'm not the most experienced STL use out there but I'm slowly getting there. One bug(?) that's plagued me of recent is the way the OS (or is it STL?) appears to relocate a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.