Home > Mobile >  Performing class method directly on the output of a function that returns data in that class (C )
Performing class method directly on the output of a function that returns data in that class (C )

Time:08-29

I'm brand new to all of this, and I'm sure I'm not quite phrasing things properly - sorry about any dumb questions and mistakes, and thanks for bearing with me! :)

That aside, here's my situation: I've created a class, a member function for that class, and another (non-member) function that returns values in that class. I know that everything works the way it's supposed to on its own - the member function returns the expected values when I use it on normal objects, and the other function returns the objects that it's supposed to - but for some reason I don't seem to get anything at all when I use them this way:

functionReturningMyObject(parameters).memberFunction(other parameters);

Is there no way to get a method to act directly on an object that is the output of another function in this way? Or does my problem lie elsewhere?

EDIT: here is my attempt at a "minimal reproducible example," as requested:

Main.cpp

#include <iostream>
#include <string>
#include "mre.hpp"

//Here are some objects
Class first (1, 'A');
Class second (1, 'B');
Class third (2, 'A');
Class fourth (2, 'D');

//Here is the function returning objects in the class
Class FindMatchingObject(int matchNumber, char matchLetter) {
  if ((first.getNumber()==matchNumber)&&(first.getLetter()==matchLetter)) {
    return first;
  }
  else if ((second.getNumber()==matchNumber)&&(second.getLetter()==matchLetter)) {
    return second;
  }
  else if ((third.getNumber()==matchNumber)&&(third.getLetter()==matchLetter)) {
    return third;
  }
  else if ((fourth.getNumber()==matchNumber)&&(fourth.getLetter()==matchLetter)) {
    return fourth;
  }
}

int main() {

   //User puts in a string consisting of: first, the old number-letter pair to identify the object they'd like to change, then second, the new number-letter pair that they'd like to change it to. In this case, I put in '1B8H' to pick 'second' and change its values to 8 and H.
  std::string userInput;
  std::cin >> userInput;
  int oldUserNumber = userInput[0] - 48;
  char oldUserLetter = userInput[1];
  int newUserNumber = userInput[2] - 48;
  char newUserLetter = userInput[3];

  //Here we see that, indeed, 'second' is being identified by the function.
  std::cout << FindMatchingObject(oldUserNumber, oldUserLetter).getNumber() << FindMatchingObject(oldUserNumber, oldUserLetter).getLetter() << "\n";

  // Here we see that, indeed, the change() method can take an object and set new values for it.
  third.change(7, 'G');
  std::cout << third.getNumber() << third.getLetter() << "\n";

  // But here, after trying to use change() on 'second' directly through the FindMatchingObject() function, the values of the object remain unchanged afterwards.
  FindMatchingObject(oldUserNumber, oldUserLetter).change(newUserNumber, newUserLetter);
  std::cout << second.getNumber() << second.getLetter() << "\n";
}

mre.hpp

#include <iostream>
#include <string>

class Class {
  char letter;
  int number;

  public:
  Class(int number0, char letter0);
  void change(int newNumber, char newLetter);
  int getNumber();
  char getLetter();
};

mre.cpp

#include <iostream>
#include <string>
#include "mre.hpp"

Class::Class(int number0, char letter0) {
  number=number0;
  letter=letter0;
}

void Class::change(int newNumber, char newLetter) {
  number=newNumber;
  letter=newLetter;
}

int Class::getNumber() {
  return number;
}

char Class::getLetter() {
  return letter;
}

Also, I know it's kind of gross having all of that stuff above the main() function, but for some reason I got compiling errors until I put them there.

CodePudding user response:

Your ‘FindMatchingObject()’ function returns a ‘Class’ object by value, thus returning a copy of the object. So, you are calling ‘change()’ on a copy of ‘second’, not on ‘second’ itself. That is why ‘second’ doesn’t change, even though ‘second’ was returned.

To fix that, your function would have to return a ‘Class’ object by reference or pointer instead.

However, do note that your function has undefined behavior if no matching object is found, as no ‘return’ gets executed in that case. Your compiler should be warning you about that. You would need to either return a default object, or throw an exception, or return ‘nullptr’ if you change the function to return a ‘Class*’ pointer.

  • Related