question about references and my broken method

I have the following method which doesn’t work:

vector<ofxDTangibleBase> closest2 (vector<ofxDTangibleBase> &listA, vector<ofxDTangibleBase> &listB) {  
    vector<ofxDTangibleBase&> returnList;  
    float closestDistance = FLT_MAX;  
    ofxDTangibleBase* closestA;  
    ofxDTangibleBase* closestB;  
    for (ofxDTangibleBase &a : listA) {  
        for (ofxDTangibleBase &b : listB) {  
            float d = distSquared(a, b);  
            if ( d == 0) {  
                // should i use & since it's allready a reference  
                return returnList;  
            if (d < closestDistance) {  
                closestDistance = d;  
                closestA = &a;  
                closestB = &b;  
    return returnList;  

I was wondering a few things:

Is there a difference in:

vector<ofxDTangibleBase> closest2 (vector<ofxDTangibleBase> &listA, vector<ofxDTangibleBase> &listB) {  


vector<ofxDTangibleBase> closest2 (vector<ofxDTangibleBase&> &listA, vector<ofxDTangibleBase&> &listB) {  

Since the vector is allready past by reference. And if there is no difference, which one should i choose.

If i remove the & in the following line, will it then make a copy when i push_back things into an vector?

vector<ofxDTangibleBase&> returnList;  

I can’t compile of my method, i get a lot of errors like:
’Pointer’ declared as a pointer to a reference of type 'ofxDTangibelBase &'
Can someone help?

the & has 2 different meanings depending where you use it:

in declarations:

  • in a parameter it means that when you call that method/function the object you pass won’t be copied and instead inside the method you’ll get a reference

  • in the declaration of a variable it means that you make that variable a reference to the original variable like:

vector<ofxDTangibleBase> returnList;  
ofxDTangibleBase & base = returnList[0];  

base is a reference to the object in position 0.

but references have to be initialized in the declaration so you can’t use a vector of references like:

vector<ofxDTangibleBase&> v;  

cause the vector will try to create a reference without a value which is illegal

the other meaning is when you use it before a variable like:

int a = 5;  
int * b = &a;  

in that case the & means give me the memory address (a pointer) of a. in this case it’s not a reference anymore but a pointer

in your code the problem is that this:

vector<ofxDTangibleBase&> returnList;   

is illegal, you can do:

vector<ofxDTangibleBase> returnList;  

and that will create a copy of each object, if you want to return references to the originals you need to create a vector of pointers instead like:

vector<ofxDTangibleBase*> returnList;  

and then of course the return type of the method also has to be a vector of pointers

thanks as always, that really helped.

One more.
I have a pointer named b.
And a method that needs a reference.
Now i always use 2 lines, first make a reference, then pass it.

            ofxDTangibleBase &rB = *b;  

Can that be done in one line?

If i try this for example (i know it looks stupid, but makes the most sense to me):


Then i get:
Non-const lvalue reference of type ‘ofxDTangibleBase’ cannot be bind to a temporary of type 'ofxDTangibleBase *'

I have that error a lot of times, and i always manage to get around it with how i do it above.
I looked up questions at stackoverflow but i have problems understanding.

Also why is there a l in lvalue?

![]( Shot 2013-11-03 at 10.45.24 PM.png)

it’s the same as before the & in this case tries to get the memory address so &*b is the same as doing nothing, you just need to do: