PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to pass array of object pointers to function?

 July 08, 2022     arrays, c++, class, function, pointers     No comments   

Issue

I am having 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.

Furthermore, 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;
}

Solution

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);
}


Answered By - JeJo
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing