• 12
• 27
• 9
• 9
• 20

# Rules of Variable References

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Lately I have been working on expanding my knowledge in C++, and I decided to actually take advantage of References rather than using pointers to objects. I ended up coming up with two questions where at the moment I didn't find much of a suitable answer. Question 1: Lets say we have an vector of some object. If I pass the reference of a local-object into the array, will the array keep the reference or will it duplicate the object? From my understanding the object will be duplicated, than once the local variable is out of scope the original object will be destroyed. Example code follows, but as I see it the object is copied than when Foo is left the original int will be lost. Of course in reality I would be using larger classes rather than simple values.
std::vector<int> myIntList;

void AppendValue(int& a)
{
myIntList.push_back(a); // Here a copy of a will be created, correct?
}

void Foo()
{
int myInt = 12;
AppendValue(myInt);
}

int main(int argc, char *argv[])
{
Foo();
return 0;
}


Question 2: Can the reference become invalid? From what I read it can but I also read somewhere on here that as long as there is a existing reference to an object, it will stay until all references are non-existant. Example code follows..
int& Foo()
{
int a = 12;
return a;
}

int main(int argc, char *argv[])
{
int &myInt = Foo();
// I am unsure if the above line would be a valid reference or an invalid reference. I would guess that it would be valid as it makes sense that a object which normally would be destroyed ends up holding existence until all references are lost.
return 0;
}


I am sorry if my questions are more 'rookie' questions but it really so far is the only thing I can think of that I haven't understood. If you get a hunch I missed out on something important please say. I have learned by looking at source, but only very little of actually reading tutorials. I have a fairly good grasp of classes, structures, templates, references, and inheritance.

##### Share on other sites
Quote:
 Question 1:
All standard library containers are safe in all such respects.

Quote:
 Question 2: Can the reference become invalid? From what I read it can but I also read somewhere on here that as long as there is a existing reference to an object, it will stay until all references are non-existant.

Nope, far from it.

References are not smart pointers or garbage collection mechanism. They also cannot change owners. References merely point to some allocated object. They cannot become owner of the data.
struct Holder {  Holder( int & var ) : a(var) {}  void print()  {    std::cout << a;  }  int &a;};Holder * foo() {  int i = 10;  return new Holder(i);};foo()->print(); // BOOM!

Never store reference unless you know it will outlive the holder. Generally never use references with dynamically allocated objects. For stack allocation, it's considerably harder to break them.

##### Share on other sites
So in a basic game I am coding, I have a primary class which needs a list of other classes which the user can 'register' by calling addEventWatch. Originally I had the argument as EventWatch& but from what I understand, I should just get the pointer of it. I actually see where this is going, and makes more sense. The only issue I can see is that if any instance of EventWatch inside of EventManager's std::vector, the instance would have to be removed out of the vector list before the next event iteration, else it would no longer be a valid pointer. It won't take much to guarantee this. Thanks for the extremely quick response!

##### Share on other sites
There is some odd rule about temporaries bound to const references not being destroyed until the end of scope, rather than the usual end of statement:

std::string foo() { return "foo"; }int main(){   const std::string &ref = foo();   // ref remains valid here?}

This is the sort of code I instinctively distrust due to my other knowledge of the always fickle C++, but I believe it is supposed to be standard.