Doubt about pointers

2

A few days ago I was doing a project in CodeBloocks, referring to classes, member functions and pointers, which is about entering 2 numbers (1 in each function) and then adding it in another function; Just like this.

#include <iostream>
using namespace std;
class retorno
{
public:
    int num1 ()
    {
        cout <<"\nescriba numero 1";
        cin >> a;
    }
    int num2 ()
    {
        cout <<"\nescriba numero 2";
        cin >> b;
    }
    int getnum1(int *ptra)
    {
       *ptra = a;
       return *ptra;
    }
    int getnum2(int *ptrb)
    {
        *ptrb = b;
        return *ptrb;
    }//////////////////////////////////////
     //Aca estaba trantando de hacerlo mediante referencia pero no resulto.
    int getsum1(int *p1)
    {
      *p1=getnum1(&a)+getnum2(&b);
      return *p1;
    }
    ///////////////////////////////
private:
int a,b;
};

int main()
{
  int d,a,s;
  retorno listo1;
  retorno listo2;
  retorno listo3;
  listo1.num1();
  listo2.num2();
  //aca imprimero los numeros introducidos mediante referencia
  cout << "\n" << listo1.getnum1(&a) << "..." << listo2.getnum2(&d);
  //aca es donde quiero que me muestre la suma de ambos numeros
  cout <<"suma"<<listo3.getsum1(&s);
}

In getnum1 I set a pointer, which serves as a buffer (that's what I understood of the concept), then I set the gets above and return it with return . At the moment of calling the functions with the object I should add but it does not work.

I have tried to match the variables in a single function with pointers and return the sum in another function but it does not work either.

    
asked by Ender Perez 16.08.2018 в 06:43
source

2 answers

2
  

In Getnum1 I set a pointer ...

We've already started badly.

Class retorno stores two numbers, a and b . What getnum1 does is copy the value of a in the pointer, no references or anything like that. Take the stored value in a , make a copy of it and store it in the memory address addressed by the pointer, as you can see clearly:

int getnum1(int *ptra)
{
   *ptra = a;    // <<--- Copia del valor de a
   return *ptra; // <<--- Copia del valor de *ptr = valor de a
}

I also do not understand, in this function, why you have to do the return , although perhaps, to understand that you do not get a reference anywhere:

int getnum1(int *ptra)
{
   *ptra = a;
   return a;
}

Now, maybe your problem is not that and you're just thinking that maybe, listo1 , listo2 and listo3 share variables ... and it's not like that. In fact, your program presents a very different result if you delete listo1 and listo2 :

int main()
{
  int d,a,s;
  retorno listo1;

  listo1.num1();
  listo1.num2();
  cout << "\n" << listo1.getnum1(&a) << "..." << listo1.getnum2(&d);

  //aca es donde quiero que me muestre la suma de ambos numeros
  cout <<"suma"<<listo1.getsum1(&s);
}

And, as I said before, it does not make much sense for the functions to return a value that they are already storing in the pointer passed as a parameter since the same result can be obtained using the variables a , d , and s :

int main()
{
  int d,a,s;
  retorno listo1;

  listo1.num1();
  listo1.num2();

  listo1.getnum1(&a);
  listo1.getnum2(&d);
  listo1.getsum1(&s);

  cout << "\n" << a << "..." << d << '\n';


  cout << "suma" << s;
}
    
answered by 16.08.2018 в 07:52
0

Thank you very much for answering. In reality the gets of num1 and num2 do not whistle anything like you say nor the returns, likewise if you remove them they do not change their result; at that time I was just trying to store the 2 numbers in pointers and display them in the main with & in another way, (I'm new in this pointers, functions and classes to be honest). With respect to the main I always had the idea that the objects could share their variables, where I was forcing that listo1 and listo2 join in the new object lista3 to give the sum but I see that no is so, so I understood the same object can have call the functions and make them work together as long as they have their corresponding variables at the time of returning them (this is noted in the getnum1 and getnum2 ).

Many gracicas !!

    
answered by 17.08.2018 в 05:32