Reputation: 110
I'm new to C++ and want to accomplish something like this but it doesn't work as I expect it to.
Object& object = Object();
I want to save the reference the the object I created in the variable object
.
How do I do this? Thanks.
Edit: I'm trying to pass the reference to the object to the constructor of another object. It has to be the reference (or pointer?) because it's code for an arduino that doesn't have much memory.
Upvotes: 3
Views: 11184
Reputation: 69922
There are two legal ways to store the variable by reference:
// o1 is const
const Object& o1 = Object();
// o2 is mutable
Object&& o2 = Object();
There is a special rule in c++ that keeps the Object alive for as long as the reference is in scope.
What you might want to do is merely store the object:
Object o = Object();
Upvotes: 5