My reply is a bit late, but I decided to post it anyway.
First of all, if you're coming from Java, be advised that in C++ every
new must have a corresponding
delete, otherwise a memory leak occurs.
*
* unless you're using smart pointers such as
std::unique_ptr.
In the code below, why are you dereferencing
tables?
By doing this you effectively pass the first element of the
tables array instead of the entire array.
1 2
|
waiters[0] = new Waiter("Steve", "1,2,3,4", *tables);
waiters[1] = new Waiter("Bob", "5,6,7,8", *tables);
|
This is probably what you wanted to do:
1 2
|
waiters[0] = new Waiter("Steve", "1,2,3,4", tables);
waiters[1] = new Waiter("Bob", "5,6,7,8", tables);
|
And then your
Waiter constructor must be changed accordingly.
1 2 3 4 5 6 7 8 9 10 11
|
Waiter::Waiter(string name, string TableList, Table *tables[]): name(name)
{
// this->name = name; // can still use initialization lists, as seen above
// use -> for accessing member data/functions from a pointer
// p->func() same as (*p).func()
cout << tables[0]->getMaxSeats() << '\n'; // don't overuse endl, it flushes the stream
cout << tables[1]->getMaxSeats() << endl;
}
|
Adding the square brackets for
*tables[]
is just syntactic sugar, the code would behave the same if we wrote
**tables
instead.
This is because in C and C++, arrays decay to pointers, that is when you use an array's name, it becomes a pointer to the first element in the array and not the array itself.
A final observation, you are overusing pointers.
My suggestion is to simply use containers, and smart pointers if you absolutely must.
http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/reference/memory/unique_ptr/