vendredi 12 février 2021

Multiple functions for a class does not store previous results

Problem

I want to create a function inside a class which function2 will use the result generated from function1. I have a small code snippet where I tried to make it easy to understand.

#include <stdio.h> 
  
class GreaterSmaller { 
public: 
    int greater, smaller; 
}; 
  
GreaterSmaller findGreaterSmaller(int a, int b) 
{ 
    GreaterSmaller s; 
    if (a > b) { 
        s.greater = a; 
        s.smaller = b; 
    } 
    else { 
        s.greater = b; 
        s.smaller = a; 
    } 
  
    return s; 
} 
GreaterSmaller print() 
{ 
    GreaterSmaller s; 
    std::cout << s.greater << s.smaller << std::endl; 
} 
  

int main() 
{ 
    int x = 4;
    int y = 3; 
    GreaterSmaller result; 
 
    result = findGreaterSmaller(x, y); 
    result = print(); // I want it to print 4 & 3
  
    return 0; 
} 

P.s Just wanted to mention I am not trying to print the result in the function2 I have created that for a demo.

Aucun commentaire:

Enregistrer un commentaire