0

I believe this could be a duplicate just because I don't knwo what exact terms should I use to google for the right thing.

I'm working with Arduino and I've written library for communication between multiple Arduinos and Raspberries.

class Machine {
    public:
        // other functions here
        Mission & create_mission(int num);

    private:
        // other variables here
        Mission _missions[10];
    };

And here is the class implementation:

    Mission & Machine::create_mission(int num) {
        Mission mission = Mission();
        mission.setNumber(num);
        _missions[_missions_count] = mission;
        _missions_count += 1;

        return _missions[_missions_count-1];
    }

As I understand, this:

    mission1 = machine.create_mission(1);

does not point to the same mission in _missions array within the class Machine. As I'm not experienced in c++, the question is - how should I implement the function to return the same Mission that's inside the _missions array?

The full code can be downloaded here (.zip): riddle.lv/code/code.zip

9
  • 2
    Definition should also return by reference. And currently, you have error as you return reference of temporary object. Commented Nov 17, 2015 at 13:27
  • Can you please be more specific? :) Commented Nov 17, 2015 at 13:29
  • Why not using std::array<Mission, 10> ? Commented Nov 17, 2015 at 13:31
  • 1
    In declaration, you have: Mission & create_mission(int num) and in definition, you have Mission Machine::create_mission(int num) (missing reference). Commented Nov 17, 2015 at 13:31
  • 1
    @Swagger it is the case, you should return the reference to your newly created object in the map instead of a reference to the temporary mission. Commented Nov 17, 2015 at 13:33

3 Answers 3

2

Something like this?

    _missions[_missions_count] = mission;
    _missions_count += 1;

    return _missions[_missions_count-1];
}

Also you need to change function signature to

  Mission& Machine::create_mission(int num)
Sign up to request clarification or add additional context in comments.

4 Comments

This won't work. I believe that it is the same as I've written it so far
I tested that - it still is not the same object when I do this: mission1 = machine.create_mission(1);
@Swagger not your whole code, but a minimal reproducible example, yes, put it in the question.
I've added the url to the question :)
1

You can either return by reference:

Declaration:

Mission & create_mission(int num);

Definition:

  Mission& Machine::create_mission(int num) {
        Mission mission = Mission();
        mission.setNumber(num);
        _missions[_missions_count] = mission;
        _missions_count += 1;

        return _missions[_missions_count - 1];
    }

Or by pointer:

Declaration:

Mission* create_mission(int num);

Definition:

Mission* Machine::create_mission(int num) {
            Mission mission = Mission();
            mission.setNumber(num);
            _missions[_missions_count] = mission;
            _missions_count += 1;

            return &_missions[_missions_count - 1];
        }

You'd normally return by reference if you expect the object to be there, which is the case here.

Currently, you're returning the reference of a temporary (mission) which is not allowed. _missions[_missions_count] = mission; Will copy your mission object to the map which will outlive the create_mission scope which is what you want.

4 Comments

When I do this using reference mission1 = machine.create_mission(1); mission1 is not the same object that's inside the class
@Swagger it is when returning the reference from the map rather than the temporary object.
@Arkadiy It doesn't? it returns a reference to the copied map object. Recheck the code.
@JameyD Right you are, deleting my comment
0

Your code creates a new Mission object and assigns it to one in your list. Then this temporary object will be returned. Furthermore the create_mission() method is declared to return a copy of a Missionobject.

You can circumvent this by working with references like this:

 Mission &Machine::create_mission(int num) {
    // create a reference to one of the Missions in the array
    Mission &mission = _missions[_missions_count];
    // work on that Mission
    mission.setNumber(num);
    _missions_count += 1
    // return the reference;
    return mission;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.