Global variable C++

Asked

Viewed 704 times

1

I have that code

int main() {
    ListaVend lista;
    criarLista(&lista);
    for(int i = 0; i <= VEND_MAX; i++){
        Vendedor vend;
        vend.codVend = i;
        vend.nome = "Vendedor "+to_string(i);
        vend.salFinal = i;
        inserLista(&lista, &vend, i);
    }
    showLista(lista);

That way it works normal. Now I wanted the Variable to be accessed outside the main too, as I can do this?

Ps.

ListaVend lista;
int main() {
    criarLista(&lista);
    for(int i = 0; i <= VEND_MAX; i++){
        Vendedor vend;
        vend.codVend = i;
        vend.nome = "Vendedor "+to_string(i);
        vend.salFinal = i;
        inserLista(&lista, &vend, i);
    }
    showLista(lista);

This way you don’t save any information on the List.

  • If you are going to use out of main in another function, you can pass it by "reference" in the same way you used in the function creatList.

  • So, just that I want to create a "menu" so I can manipulate this list. For example, access main calls menu, from menu access to insert option, after inserting back to menu... understood?

  • 2

    Only with this passage can not know what is happening. But anyway do not do it. There is no reason to do. You have every reason not to.

  • From what I could see only with this excerpt "should" work; not knowing what this creatLista() works is difficult but, I think, which list is a list of "Seller" no? Why don’t you "use vector<seller> list" ?

  • Sorry, here is my complete code: http://pastebin.com/eX9dZPqk It is that I am learning lists in college, ai queria fazer esse "menu" para poder deixar mais bonito e funcional

  • Try passing the list as pointer in showlist: showList(&list);

  • See if it helps: http://answall.com/q/99551/101 But reinforcement, ideally not. And I think it’s a shame that in a college you teach to mix C with C++.

  • Got it, Perry, I’m gonna try to implement this. And Bigown I’m gonna get a better handle on this.

Show 3 more comments

1 answer

3

Overall your program is right, with the following reservations:

these functions

bool cheiaLista(ListaVend);    // BAD
bool vaziaLista(ListaVend);    // BAD
void showLista(ListaVend);     // BAD

should be declared with pointer or reference parameters, thus

bool cheiaLista(ListaVend*);   // GOOD
bool vaziaLista(ListaVend*);   // GOOD
void showLista(ListaVend*);    // GOOD

or so

bool cheiaLista(ListaVend&);   // GOOD
bool vaziaLista(ListaVend&);   // GOOD
void showLista(ListaVend&);    // GOOD

to avoid copies in passing parameters to these functions, which is a business extremely inefficient (in addition to, in the general case, obliging to implement the copying constructors).

In addition there is a logic error: this here

for (int i = 0; i <= VEND_MAX; i++) {

it should be like this

for (int i = 0; i < VEND_MAX; i++) {

The way you’ve implemented your functions seems like it’s going to work, but this comparison i <= VEND_MAX in C++ is counter-intuitive, hinders reasoning, and is creating an extra seller that is not being included in the list.

  • So, in the functions of full, empty and show did not use reference because the teacher said "Only use reference if you change something".

  • If you do not use reference or pointer will be made a copy. If the parameter is simple (int, pointer, etc.) this is no problem, but if you have an object with (e.g.) 50Kb then a copy of that 50 Kb object will be made to pass as parameter to the function! There are other considerations yet, but only this copy fact is enough to (almost) always use pointer or reference to class instances (and remember: a struct is a class), strings, etc, "large data types".

  • One more thing I saw in your code, this is reductive: return (lista.n == VEND_MAX) ? true : false;, use return (lista.n == VEND_MAX); ...actually the parentheses are optional, it could be like this return lista.n == VEND_MAX; but it depends on the person

Browser other questions tagged

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