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.
waiters = new Waiter("Steve", "1,2,3,4", *tables);
waiters = new Waiter("Bob", "5,6,7,8", *tables);
This is probably what you wanted to do:
waiters = new Waiter("Steve", "1,2,3,4", tables);
waiters = new Waiter("Bob", "5,6,7,8", tables);
And then your Waiter
constructor must be changed accordingly.
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->getMaxSeats() << '\n'; // don't overuse endl, it flushes the stream
cout << tables->getMaxSeats() << endl;
Adding the square brackets for
is just syntactic sugar, the code would behave the same if we wrote
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.