Solved

# reference to a vector

Posted on 2007-07-30
196 Views
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
Question by:DJ_AM_Juicebox

LVL 30

Accepted Solution

The code sees both a and b as the same.
0

LVL 30

Expert Comment

>>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

LVL 30

Expert Comment

>>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

Author Comment

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

Thanks
0

LVL 86

Expert Comment

>>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

LVL 30

Expert Comment

>>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

LVL 30

Expert Comment

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

Author Comment

ok i see thanks.
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

#### 779 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!