OOP design question

This is a discussion on OOP design question within the C++ Programming forums, part of the General Programming Boards category; I've got a quick question about OOP design. Would you say that it's good practice to put code into a ...

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    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
    Last edited by codec; 05-11-2004 at 07:00 AM.
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks for the help Magos. I'm almost completely new to OOP so it just didn't come to mind. Practice makes perfect =)

    dan
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program design question
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2005, 07:18 PM
  2. program design question
    By Chaplin27 in forum C++ Programming
    Replies: 0
    Last Post: 06-23-2005, 06:58 PM
  3. OOP Design
    By filler_bunny in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2003, 05:37 AM
  4. Java and OOP design help???
    By mart_man00 in forum Tech Board
    Replies: 10
    Last Post: 08-24-2003, 04:52 AM
  5. design question: opinion
    By ggs in forum C Programming
    Replies: 2
    Last Post: 01-29-2003, 10:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21