reference to a vector

Hi,

This is a bit strange - is the following ok:

vector<int> a(100, 15);
vector<int> &b = a;

now does b just point to vector a? So if I modify a, that will be reflected when dumping values of b? And when modifying values through b, they will be reflected in a? And b does not allocate any actual memory, right?

Normally I would just use a pointer like:

    vector<int>* p  = &a;

but I'm trying to find out for someone who hates using the '->' operator etc with vectors. Strange I know,

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
 
AxterCommented:
The code sees both a and b as the same.
0
 
AxterCommented:
>>So if I modify a, that will be reflected when dumping values of b?
Yes

>>And when modifying values through b, they will be reflected in a?
Yes

0
 
AxterCommented:
>>And b does not allocate any actual memory, right?

It doesn't allocate any additional memory, other then the address used to point to a.


>>but I'm trying to find out for someone who hates using the '->' operator etc with vectors. Strange I know,

I hate using pointers, and C++ experts recommend using reference over pointers when possible.  It's not always possible (practical) to avoid pointers, but when you can, you should.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
DJ_AM_JuiceboxAuthor Commented:
ok, so basically the example is equivalent to a pointer though, right? I don't really see a difference?

Thanks
0
 
jkrCommented:
>>ok, so basically the example is equivalent to a pointer though, right?

Lokks like that, but there are subtle differences. See http://www.embedded.com/shared/printableArticle.jhtml?articleID=9900082 ("References vs. Pointers")
0
 
AxterCommented:
>>ok, so basically the example is equivalent to a pointer though, right? I don't really see a difference?

There are some differences between a pointer and a reference.

You can not change what a reference is pointing to, where as you can change what a pointer is pointing to.

string x = "I'm X";
string y = "I'm Y";
string z = "I'm Z";
string *p1 = &x;
p1 = &y; //You can change what a pointer is pointing to
p1 = &z;

You can't do that with a reference.
string &r1 = x;
r1 = y; //Here, r1 will still be pointing to x, but it will not have the value of y.

cout << x << endl;  //Out put will be "I'm Y"
cout << r1 << endl;  //Out put will be "I'm Y"

You also can not create a reference without having it point to something.

string &r1; //This will NOT compile
0
 
AxterCommented:
Although technically you could delete what a pointer is pointing to and you could have a reference point to NULL, it's not considered good programming practice.

So generally, you don't have to worry about a reference pointing to NULL, and you don't have to worry about cleaning up memory.

That's why it's better to use a reference, when you know the object exist, and you know that the scope is not responsible for deletion.

void foo(string &r1); //Pass by reference if foo is not responsible for deleting the object, and doesn't need to check for NULL.

void foo(string *r1); //Pass by pointer if foo needs to check for NULL, and/or needs to delete the object when it's done with it.
0
 
DJ_AM_JuiceboxAuthor Commented:
ok i see thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.