Passing an array of pointers

I am trying to pass an array that points to a Worker class object that I have created. I need another class to access the object parameters so I am passing it in a function. Having some difficulty.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  
class Manager:public Employee
{
public:
	Manager();

	void LoadEmployee();
	string getName();
	string getEmail();
	string getTelNum();
	//string getLocation();

	virtual ~Manager();

	

private:

	static const int kingston_employees = 20;//find out how to change the size without have to modify code
	static const int mobay_employees = 10;

	//Array of pointers to the Worker Object type
	Worker *arr_king[kingston_employees + 1]; 
	Worker *arr_mobay[mobay_employees + 1];

	pass(arr_king);//FUNCTION TO PASS POINTER TO OBJECTS 
here is a stylized example with two embellishments (so to speak): (a) std::vector instead of C-style array, (b) std::unique_ptr instead of C-style pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <utility>

struct Student
{
    std::string m_name;
    int m_age;
    Student(const std::string& name, const int& age)
    : m_name(name), m_age(age){}
    ~Student(){std::cout << "Goodbye " << m_name << " aged " << m_age << "\n";}
};
std::ostream& operator << (std::ostream& os, const Student& s)
{
    os << s.m_name << ": " << s.m_age << "\n" ;
    return os;
}

struct Teacher
{
   std::string m_name;

   void print_students(const std::vector<std::unique_ptr<Student>>& students) const;
};

int main()
{
    std::vector<std::unique_ptr<Student>> students{};

    students.push_back(std::move(std::make_unique<Student>("John", 18)));
    students.push_back(std::move(std::make_unique<Student>("Jane", 16)));

    Teacher t{std::string("Alex")};
    t.print_students(students);
}

void Teacher::print_students(const std::vector<std::unique_ptr<Student>>& students) const
{
    std::cout << m_name << " has the following students \n";
    for (const auto& elem : students)
    {
        std::cout << *elem ;
    }
}

Topic archived. No new replies allowed.