Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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
0
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
  • 5
  • 2
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now