Is it possible to have a List of a class inside that class??
Lets say I have my Clients class in the file " Client.h "
Client.h
1 2 3 4 5 6 7 8 9
class Client{
public:
Client();
Client(const string& firstName, const string& lastName);
//....... More functions
private:
list<Client>myClients; <===== This line bothers me
//....
};
Is it possible that this code works?
If yes, how could I implement it or push_back the new client from the constructor?
If you want to add each object that is created to this list you probably want to use a list of pointers instead.
I also guess you want all Clients to share the same list. In that case you should declare it as a static variable.
static list<Client*> myClients;
And then you also have to define it outside the class (in the a source file), like so:
list<Client*> Client::myClients;
In the constructor you can use the this keyword to get a pointer to the current object.
myClients.push_back(this);
Don't forget to remove the pointer in the destructor.