Reputation: 940
I have two functions.
function1 calls function2, which returns a reference to an object, that I then use to call a public function.
function1(function2).returned_object.mymethod();
This is a fairly unwieldy way to do things, but it works.
Now I have a problem where I need to call one method, then another method, so I need a temporary object.
ClassName temp_obj = function1(function2);
temp_obj.method1();
temp_obj.method2();
My question is, how can I declare a temporary object that will store the object that returns by reference. I'm guessing I need a pointer.
The other problem is, temp_obj is contained in a vector, and I'm concerned about conflicts there and memory leakage. If I use a pointer, will I then have to use a destructor also?
Here is the relevant code:
bool Machine::perform_function(std::vector<Job>& jobs) {
bool add = true;
if (can_fail) {
add = coin_toss();
}
for (int i = 0; i < my_jobs.size(); i++) {
if (add) {
find_job(my_jobs[i], jobs).toggle(name, item_name, true);
}
if (!add) {
find_job(my_jobs[i], jobs).toggle(name, item_name, false);
}
}
return add;
}
Job& Machine::find_job(std::string jobname, std::vector<Job>& jobs) {
for (int i = 0; i < jobs.size(); i++) {
if (jobs[i].is_job(jobname)) {
return jobs[i];
}
}
}
Upvotes: 0
Views: 134
Reputation: 36597
Assuming it is function1()
that returns a reference (your question is unclear on that), simply use a reference
ClassName &temp_obj = function1(function2);
temp_obj.method1();
temp_obj.method2();
If function1()
returns a const
reference, than temp
also needs to be const
.
The limitation of this is that, once created, a reference cannot be reseated (made to refer to a different object). For example, in ;
ClassName &temp_obj = function1(function2); // initialisation
temp_obj = function1(some_other_function); // reassignment
temp_obj.method1();
temp_obj.method2();
the reassignment will not reseat temp
, even if function1()
returns a different reference on the second call. Instead, the object being referred to will be assigned (assuming it has a working assignment operator). If you want a variable that can refer to different objects over time, you need a pointer.
ClassName *temp_obj = &function1(function2); // assumes function1() returns a reference
temp_obj = &function1(some_other_function); // reassigns the pointer
temp_obj->method1();
temp_obj->method2();
Upvotes: 1
Reputation: 95
Your question is unclear. But if you want to make a pointer to a function or member function there is a FAQ about such variables here.
https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs-memfnptr-types
Upvotes: -1
Reputation: 63471
It's perfectly fine to store the result as a reference:
ClassName & temp_obj = function1(function2);
temp_obj.method1();
temp_obj.method2();
Your other question here:
The other problem is, temp_obj is contained in a vector, and I'm concerned about conflicts there and memory leakage.
It's okay to return jobs[i]
as a reference. The operator[]
on a vector returns a reference itself. Provided you don't modify the vector in such a way as to change what that reference points at, and you are not storing a reference to an object that has been destroyed, you'll be fine to use it.
When your temporary reference goes out of scope, nothing happens. It's just a reference.
Upvotes: 1
Reputation: 133587
You need a temporary variable, but that temporary variable could be a reference so no copy is done and you are safe to use it.
But still better, you could use <algorithm>
functions to do what you need without having to reinvent the wheel:
std::string name = ...;
auto it = std::find_if(begin(jobs), end(jobs), [&name] (const Job& job) { return job.is_job(name); });
if (it != jobs.end())
{
it->method1();
it->method2();
}
Upvotes: 2