How to call the constructor within a method

Asked

Viewed 73 times

2

I have a simple college exercise to mess with the vector. Basically these are the actions that we should do:

    cout << "\tMexendo com Vetor\n";
    cout << "\n\nEscolha uma das opcoes para fazer com o Vetor:";
    cout << "\n01 - Imprimir ";
    cout << "\n02 - Adicionar";
    cout << "\n03 - Limpar ";
    cout << "\n04 - Localizar um numero";
    cout << "\n05 - Excluir o numero de uma posicao";
    cout << "\n06 - Trocar o numero de posicao";
    cout << "\n\n00 - Encerrar o programa";

In option "03 - Clear" you have to clear the whole vector.

For this I would like to call the Constructor within my method, after all it starts my object with the clean vector, doing the same as the chosen option!

My method Clear:

void Vetor::clear_vet () {
    while (contador != 0) { del_pos(0); }
}

Currently it keeps calling another method (to delete the position) and keeps deleting the first position until there is no more element (when the counter reaches 0)

The idea I had would be like this:

void Vetor::clear_vet () {
    Vetor();
}

my file .hpp in case you have any doubts:


#ifndef VETOR_HPP
#define VETOR_HPP

#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

using namespace std;


class Vetor {    
    private:
        int contador, i;
        int vet[10];
    public:
        Vetor () { 
            contador = 0; 

            for (i = 0; i < 10; i++) { vet[i] = 0; } 
            };
        void print ();
        void add_vet (float num);
        bool del_pos (int pos1);
        void clear_vet ();
        int loc_num (float num);
        bool change_pos (int pos1, int pos2);
        bool valid(int pos);
};      

#endif // VETOR_HPP

1 answer

2


Apparently that’s what you want:

void Vetor::clear_vet() {
    contador = 0; 
    for (int i = 0; i < 10; i++) vet[i] = 0;
}

I put in the Github for future reference.

Eventually, just to make the code easier to maintain, like something more advanced, but you don’t have to if you don’t understand why you’re doing it, maybe want to have a private method to put this code and call inside the above method and the constructor this new method that serves just to avoid repetition, so the information on how to do the initialization becomes canonical. If you find it difficult to understand, do not, use the way I did above.

Calling the constructor would create another new object and that’s not what you want, don’t do it.

Depending on the situation it is not all bad to do what you were already doing, maybe there is no loss of efficiency if the compiler thinks it should optimize, and if it does not optimize it is probably because it does not need. But only if the method is very well done and is thought to clean the last element at no additional cost, but as I’m almost sure that is not thought for so it is better not to use, but it is hinted that the method does not look good.

Do not make a variable that is only used locally integral part of the object.

There are indications that there are other problems there, but without seeing the whole, we would not be able to guarantee it. It might even help to learn some mechanisms, but it’s probably de-signaling how to create a data structure correctly, keep this in mind not to fix it as if it were correct, because working is different from right star.

  • Did not understand, call another method within a method eh wrong? The part that should not be called the Builder again I understood. In the case of the local variable, I see no other way to verify how many elements have already been placed, besides that this variable tb is used in other methods to do the other operations..

  • No, I didn’t say that. You don’t need i to find out how many elements have already been used.

  • What would be another way to do it?

  • The way I did.

  • I get it, obr !!

Browser other questions tagged

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