-
OOP design question
I've got a quick question about OOP design.
Would you say that it's good practice to put code into a class' constructor and destructor that refers to variables/objects outside of that class?
For example, say I had an emloyee class and also had an STL list of pointers to all the employee objects. Would it then be good practice to add and remove the employee from the list from within the constrcutor/destructor, or should that really be done outside? The impression I've always got is that contructors and destructors are supposed to be for initialising/destroying the inner workings of a class, and the user of it should never need to know what's happening.
By manipulating variables outside of the class it becomes less modularised and means you have to know something about how the functions are doing what they are. If someone else were to use my class they may find things a bit odd without reading the class code, which kind of defeats the object of it all :)
Any opinions greatly appreciated. I hope I've explained it pretty well, because it is a bit of a blurry issue rather than a straightforward problem.
Thanks,
Dan
-
Bad. The class would then be dependent on those global variables.
Why not make that list of employees a static member of the class and add to/remove from the list in the constructor/destructor. If you need to access the list outside the class, make a member function returning a constant reference to that list.
Might be better ways, this was the first one that popped into my head.
-
Thanks for the help Magos. I'm almost completely new to OOP so it just didn't come to mind. Practice makes perfect =)
dan
-
>>Why not make that list of employees a static member of the class and add to/remove from the list in the constructor/destructor.
Yeah, that was what I was going to suggest. I've done that a couple times (static vector of pointers), although I think once I scrapped the design and did something else instead.