Home > Net >  How to pass array of object pointers to function?
How to pass array of object pointers to function?

Time:06-16

I am having a trouble passing an array of object pointers from main() to a function from different class.

I created an array of object pointers listPin main() and i want to modify the array with a function editProduct in class Manager such as adding new or edit object. I want to pass the whole listP array instead of listP[index]. How to achieve this or is there any better way? sorry I am very new to c .

#include <iostream>

using namespace std;

class Product{
    protected:
        string id,name;
        float price;
    public:
        Product(){
            id = "";
            name = "";
            price = 0;
        }
        Product(string _id, string _name, float _price){
            id = _id;
            name = _name;
            price = _price;
        }
};

class Manager{
    protected:
        string id,pass;
    public:
        Manager(string _id, string _pass){
            id = _id;
            pass = _pass;
        }
        string getId () const{
            return id;
        }
        string getPass() const{
            return pass;
        }
        void editProduct(//array of listP){
        //i can edit array of listP here without copying 
        }
};

int main()
{
    int numProduct = 5;
    int numManager = 2;
    Product *listP[numProduct];
    Manager *listM[numManager] = {new Manager("1","alex"), new Manager("2", "Felix")};
    bool exist = false;
    int index = 0;
    
    for(int i = 0; i < numProduct; i  ){ //initialize to default value
        listP[i] = new Product();
    }
    string ID, PASS;
    cin>> ID;
    cin>> PASS;
    
    for(int i = 0; i < numManager; i   ){
        if(listM[i]->getId() == ID  && listM[i]->getPass() == PASS){
        exist = true;
        index = i;
        }
    }
    
    if(exist == true){
        listM[index]->editProduct(//array of listP);
    }
    
    
    
    
    return 0;
}

CodePudding user response:

Since the listP is a pointer to an array of Product, you have the following two option to pass it to the function.

  1. The editProduct can be changed to accept the pointer to an array of size N, where N is the size of the passed pointer to the array, which is known at compile time:

    template<std::size_t N>
    void editProduct(Product* (&listP)[N])
    {
         // Now the listP can be edited, here without copying 
    }
    
  2. or it must accept a pointer to an object, so that it can refer the array

    void editProduct(Product** listP)
    {
        // find the array size for iterating through the elements
    }
    

In above both cases, you will call the function as

listM[index]->editProduct(listP);

That been said, your code has a few issues.

  • First, the array sizes numProduct and numManager must be compiled time constants, so that you don't end up creating a non-standard variable length array.
  • Memory leak at the end of main as you have not deleted what you have newed.
  • Also be aware Why is "using namespace std;" considered bad practice?
  • You could have simply used std::array, or std::vector depending on where the object should be allocated in memory. By which, you would have avoided all these issues of memory leak as well as pointer syntaxes.

For example, using std::vector, you could do simply

#include <vector> 

// in Manager class
void editProduct(std::vector<Product>& listP)
{
    // listP.size() for size of the array.
    // pass by reference and edit the  listP!
}

in main()

// 5 Product objects, and initialize to default value
std::vector<Product> listP(5);
std::vector<Manager> listM{ {"1","alex"}, {"2", "Felix"} };

// ... other codes
for (const Manager& mgr : listM)
{
    if (mgr.getId() == ID && mgr.getPass() == PASS)
    {
        // ... code
    }
}

if (exist == true) {
    listM[index]->editProduct(listP);
}

CodePudding user response:

You cannot have arrays as parameters in C , you can only have pointers. Since your array is an array of pointers you can use a double pointer to access the array.

void editProduct(Product** listP){

and

listM[index]->editProduct(listP);

Of course none of these arrays of pointers are necessary. You could simplify your code a lot if you just used regular arrays.

Product listP[numProduct];
Manager listM[numManager] = { Manager("1","alex"), Manager("2", "Felix")};

...

for(int i = 0; i < numManager; i   ){
    if(listM[i].getId() == ID  && listM[i].getPass() == PASS) {
    exist = true;
    index = i;
    }
}

if(exist == true){
    listM[index].editProduct(listP);
}
  • Related