Pass pointer as C++ function parameter

Asked

Viewed 640 times

0

Good morning guys, I have the following problem: I have a class that represents a List, and from it I created three objects (I don’t know which one to give) list1(5), Lista2(5), list3(10).

    template<class T>class Lista{
        private:
            T *itens;
            int ultimo, maxTam;

        public:
            Lista(int maxTam);
                    void insere(T item);
    };

    template <class T> Lista<T>::Lista(int maxTam){
        this->maxTam = maxTam;
        this->itens = new T[maxTam];
        this->ultimo = 0;
    }

    template <class T> void Lista<T>::insere(T item){
        if(ultimo == maxTam){
            cout<< "Lista Cheia!"<<endl;
        }else{
            this->itens[this->ultimo] = item;
            this->ultimo++;
        }
    }

int main(){
    Lista<int> lista1(5); //esta lista ja está cheia
    Lista<int> lista2(5); //esta lista já está cheia
    Lista<int> lista3(10); //esta lista deve receber o conteudo das duas listas anteriores intercalados.
    return 0;
}

Suppose I’ve filled the first two lists, like copying their items to row 3, passing them as a function parameter? I did the function down, but it doesn’t work.

    //Aqui o protótipo do método.
    template<class T> T Lista<T>::MisturaListas(Lista<T> *l1, Lista<T> *l2, Lista<T> *l3){

        for(int i = 0; i < 5; i++){
            l3->insere(l1[i]);
            l3->insere(l2[i]);


        }
   }


//Aqui a parte que fica na classe
T MisturaListas(Lista<T> *l1, Lista<T> *l2, Lista<T> *l3);

//Aqui o main
int main(){
        Lista<int> lista1(5); //esta lista ja está cheia
        Lista<int> lista2(5); //esta lista já está cheia
        Lista<int> lista3(10); //esta lista deve receber o conteudo das duas listas anteriores intercalados.
        lista3.MisturaListas<int>(lista1, lista2, lista3);
        return 0;
    }

I hope you understand, if you can help me,.

1 answer

1

Only a few adjustments in the question’s source code have produced the result, see:

New get method():

T get(int i);

This method is required to get items that have been added to the list, as the List class encapsulates an array, which in turn is not accessible in conventional mode using [].

Signature of the Mixed method():

T MisturaListas(Lista<T> l1, Lista<T> l2, Lista<T> l3);

This method could not receive pointers to List.

Correction in the loop of the Mixed method():

for(int i = 0; i < 5; i++){
    l3.insere(l1.get(i));
    l3.insere(l2.get(i));
}

As cited above, the métoto get() was created to access items entered in the list.

At the end the new source code looks like this (see running on ideone)

#include <iostream>

using namespace std;

template<class T>class Lista{
    private:
        T *itens;
        int ultimo, maxTam;

    public:
        Lista(int maxTam);
        void insere(T item);
        T get(int i);
        T MisturaListas(Lista<T> l1, Lista<T> l2, Lista<T> l3);

};

template <class T> Lista<T>::Lista(int maxTam){
    this->maxTam = maxTam;
    this->itens  = new T[maxTam];
    this->ultimo = 0;
}

template <class T> void Lista<T>::insere(T item){
    if(ultimo == maxTam){
       cout<< "Lista Cheia!"<<endl;
    }else{
        this->itens[this->ultimo] = item;
        this->ultimo++;
    }
}

template<class T> T Lista<T>::get(int i){
    return itens[i];
}

//Aqui o protótipo do método.
template<class T> T Lista<T>::MisturaListas(Lista<T> l1, Lista<T> l2, Lista<T> l3){

    for(int i = 0; i < 5; i++){
        l3.insere(l1.get(i));
        l3.insere(l2.get(i));
    }
}

int main()
{
    Lista<int> lista1(5); //esta lista ja está cheia
    lista1.insere(0);
    lista1.insere(1);
    lista1.insere(2);
    lista1.insere(3);
    lista1.insere(4);
    for(int _i = 0; _i < 5; _i++){
        cout<<lista1.get(_i);
    }

    cout<<endl;
    Lista<int> lista2(5); //esta lista já está cheia
    lista2.insere(5);
    lista2.insere(6);
    lista2.insere(7);
    lista2.insere(8);
    lista2.insere(9);
    for(int _i = 0; _i < 5; _i++){
        cout<<lista2.get(_i);
    }

    Lista<int> lista3(10); //esta lista deve receber o conteudo das duas listas anteriores intercalados.
    lista3.MisturaListas(lista1, lista2, lista3);

    cout<<endl;

    for(int _i = 0; _i < 10; _i++){
        cout<<lista3.get(_i);
    }
    cout << endl << "Hello world!" << endl;
    return 0;
}

For the rest, everything is ok.

  • Fabio just didn’t understand how this get() method works, because I did a print() method to print the three lists, he printed the first two and returned that the list3 is empty :S.

  • The method get() aims to obtain a particular item from the list and with it you expose the ability to manipulate the list by whoever is using it. It is so because who uses an instance of Lista does not know that within this class there is an array. This way you promote two important items in POO: low coupling and high cohesion.

Browser other questions tagged

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