Pointers per parameter in C++

Asked

Viewed 324 times

0

I’m having a problem with the pointer on customers. I call the function newCustomer(), that inserts nodes in the list hanging on customers. The problem is that every time the program leaves the function newCustomer(), customers comes true again nullptr.

Below is the function call, in the main():

case 1:
  newCustomer(customers);
break;

The function newCustomer() is this:

void newCustomer(Cliente *customers){
 int num;
 string nome;
 Cliente *qwe, *next;
 cout << "Numero de clientes: ";
 cin >> num;
 qwe = customers;
 if(customers == nullptr){
   cout << "oi" << endl;
   customers = new Cliente();
   cout << "Nome do cliente: ";
   setbuf(stdin, 0);
   getline(cin, nome);
   customers->setName(nome);
   customers->setid(num);
   qwe = customers;
   num--;
 }
 while(qwe->getProx() != nullptr){
 qwe = qwe->getProx();

 }
 while( num != 0){
   next = new Cliente();
   qwe->setProx(next);
   qwe = qwe->getProx();
   cout << "Nome do cliente: ";
   setbuf(stdin, 0);
   getline(cin, nome);
   qwe->setName(nome);
   qwe->setid(num);
   num--;
 }
}

I would also like to ask for some tips on how to improve my code. I’m starting at C++ now, I accept all criticism :)

2 answers

1

On the line

customers = new Cliente();

I put in the Github for future reference.

you are changing the content of a local variable, when you finish running the function this variable ceases to exist and its value disappears. For some reason you seem to think that this value survives even after the end of the function, but it doesn’t happen.

It would be possible to use Cliente **customers as a parameter there you change the variable object and not the variable itself, ie you use a indirect to solve the problem. Then all access to the final object will be given by an intermediate object which is a pointer to the final object. This pointer for being a passed object can be changed and will be reflected outside. Of course, you have to pass the argument appropriately and the access to the final object has to be always through this pointer, then it works. Or you can return the object created there instead of returning void, what can be simpler.

Since you asked for tips There they go:

In fact this is all programming in C using C++, in real C++ none of this is considered right, it has much better mechanisms that even manages the memory automatically (this case has expensive that will leak memory, at least encourages).

A function that takes care of the insertion and the user interface is not appropriate. The functions should have a responsibility, there are two. This can create maintenance problems. Exercise does not cause, but if you want to learn It is better to get used to it.

The code seems to do some unnecessary things.

I could use some formatting of the code, it might seem silly more help well and usually use in my talk:

Enquanto você não souber o que cada caractere do seu código faz, até mesmo o espaço em branco, você ainda não sabe programar

Nor did I mention that it scares me to create a single client in a plural name variable. Proper names help a lot to give understanding in the code.

  • I get it. Really, like you said, I learned to code in C and I can’t disguise kkkk. In C I always passed pointers by reference like that, I thought here would also work. I was curious to know about the real C++ . What am I missing? I program this way but I want to learn new techniques. I watch some video lessons on Youtube, even though I think they are weak, because they are what I have. Recommends some material where I can program with a more formal, more professional structure?

  • C doesn’t work that way either. You’re missing a lot, C+= is a totally different language that just happens to compile C codes. Virtually all C++ video classes are bad and teach you all wrong. Actually this is true for programming in general, there is very little with quality. I suggest looking for good books, good courses, but generally I do not indicate anything, has some indication in the description of tag. I just wouldn’t start doing things without understanding why it works or not. It will continue to follow cake recipes passed by third parties without learning.

0


That’s why you’re starting customers within the function.

Think of it this way: the customers that you have out of function is a pointer that points to the address 10000. The customers that you have within the function is a copy of the other pointer, it is allocated in a different position, but also points to the address 10000, or has the same value.

If at any point in the function you change the value that exists in the position 10000, both of them customers will be pointing to this address, and therefore the value of this address will reflect on both pointers.

However you are not changing the value in the address 10000, you are changing the address to which the pointer customers inside the function is pointing. Now the customers out of function continues pointing to 10000, but the customers inside the function points to 20000, and therefore the values are no longer reflected.

If you want the value of customers within the function always reflects on the customers out of function, you need to pass a pointer to a pointer, Cliente **customers. Or at least that’s what you would need to do in C. In C++ there is a more practical way, you can pass the reference of a variable, so the variable within the function is treated as being exactly the same as the one outside the function. All you need to do is add one & in the argument.

void newCustomer(Cliente *&customers) {
  //...
}
  • Opa liked this technique. Very simple and lean. Would you know to tell me if using it causes slowness, extra memory expenditure or any other bad consequences?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.