I know it might be a duplicate of this: Changing array inside function in C, but I read it thoroughly and there's one situation that I'm wondering:
void addCustomer(Customer*** array, int length, char type) {
for (int i = 0; i < length; i++) {
Customer *c1 = new Customer();
switch (type) {
case '1':
c1 = new LowCustomer();
break;
case '2':
c1 = new MiddleCustsomer();
break;
case '3':
c1 = new HighCustomer();
break;
case '4':
c1 = new VIPCustomer();
break;
}
(*array)[i] = c1;
}
LowCustomer, MiddelCustomer, HighCustomer and VIPCustomer classes are all derived from Customer class.
So, as I had to use the new keyword, the array now had to be ***. Is there a better way? Because my code keeps producing runtime error with 0xcdcdcdcd on c1.
My code in main looks like this:
Customer*** low = new Customer**[10];
addCustomer(low, 10, 'c');
P.S. Oh! BTW I know using vector would make it way easier, but I really want to use pointer for this one.
Customer *c1 = new Customer();and then specifying later the type, why don't you trying using the Strategy Design Pattern . There are plenty of examples (ducks are the most understandable thing).