473,512 Members | 15,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why vector<int&> is not allowed?

Hi,

Could you explain why vectora<int&> is not allowed?
For example, the following prgram will be given compilation error.

#include <vector>
using namespace std;

int main()
{
vector<int&> myvec;
return 0;
}
Thanks!

Andy

Jul 23 '05 #1
2 1697

"Andy" <gn***@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,

Could you explain why vectora<int&> is not allowed?
Because vector (and other containers) can only store
objects, and a reference is not an object. It's simply
an alternative name for an existing object. Containers
store objects, not names.
For example, the following prgram will be given compilation error.
And rightly so.

#include <vector>
using namespace std;

int main()
{
vector<int&> myvec;
return 0;
}


BTW why do you feel the need to try to store a
collection of references? You can (and often
should) use a reference to a container, e.g.
as a function parameter, which eliminates the
need for a copy operation when the function is called
(C++ passes function arguments by value by default).

-Mike
Jul 23 '05 #2
might you be wanting to do

int main()
{
vector<int*> myvec;
return 0;
}

instead? Not sure why you would need an array of references. pointers
tho, that could be useful.

Jul 23 '05 #3

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

Similar topics

7
12352
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
7
8204
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
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...
1
7093
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...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.