Thread: object oriented programming

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    object oriented programming

    Hello..

    Does 100% object oriented programming mean I should NOT have any global variables?
    At the moment I use class session, where I keep all main variables (settings) for my program and I dont have any global variables.. When I want to do some work with another class/function I have to pass session reference/pointer so it can read settings..

    I could just make all things in session global, because I probably wouldnt specify them twice, so I wouldnt have to pass the reference to this class each time (which is stupid)..

    What do you think? How do you guys do this?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I haven't used a global variable in almost a decade except in simple testing apps.

    In general, when you do OOP design, try to design it such that each variable is only needed to be accessed by a single class, and make the variable a private member of that class.

    For things like random numbers, I usually would have an RNG class, with various functions to get ints in a given range or floats in a given range. I'd have a static member function to seed the generator (if the generator was itself an object, it would be a private static member variable), or alternately I'd have the first call to the constructor do the seeding and every subsequent call not seed the generator.

    Then each class that needs random numbers can create and destroy as many object as they like.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    In my case I have a few instances of some class that should be able to access variables in main (session) class.. So if I want them to be able to access these member I have to pass the reference to the main (session) class, which sounds newbie/stupid, since I wont be using X main classes. Not sure what should I do, so I consider using global variables..

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you need multiple instances of one class to have access to the same variable, make it a static private variable of that class.

    In general if you have multiple classes which need direct access to the same variable, you probably have a poor object design. You should probably rethink how you are dividing your problem into objects and see if there is a better way to do things.

    In general objects shouldn't directly need to share data; one object (or class, in the case of static variables) should "own" the data, and anything else that needs access should use the public interface of the class.

    You can also diagram how objects should interact so that you minimize needless coupling of classes.
    Last edited by Cat; 11-05-2006 at 05:56 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I can think of very few cases where global variables are a good idea. In those few cases, I would almost certainly wrap them inside a namespace. constants such as pi, or values which impact a wide variety of things - eg, the rate of VAT in an accountancy system.

    Passing your variables by reference sounds by far the better option IMHO. If global variables seem the only way forward, then perhaps a different design approach is needed.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Cat
    If you need multiple instances of one class to have access to the same variable, make it a static private variable of that class.

    In general if you have multiple classes which need direct access to the same variable, you probably have a poor object design. You should probably rethink how you are dividing your problem into objects and see if there is a better way to do things.

    In general objects shouldn't directly need to share data; one object (or class, in the case of static variables) should "own" the data, and anything else that needs access should use the public interface of the class.

    You can also diagram how objects should interact so that you minimize needless coupling of classes.
    So you are saying if I have to access my programs settings (main class) with multiple classes I have a poor object design? Lets say I have a huge map of settings/data in my session class (main class).. Now I have to call a function in some other class that will use that data, would it be smarter to copy data to each class that will use it? I doubt so. Would passing a reference of data to classes be a bad idea? (are there better ideas?) It sounds bad, but im not sure because I do not have that much experience.

    Thanks guys

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you have, say, a Window object, and the setting you're reading is a property of a window, then you need to copy that data to the Window object. Passing references is a bad idea; it strongly couples the classes and makes changing implementations very hard. One class should not know the format in which another class stores data; it should only know the public (and protected, for child classes) interfaces that class presents to the outside world.

    In general, each object should store all relevant info about its own state. If that means you need to copy data from a settings class to your other class, then that's the best choice. When you answer the question of which object logically "owns" that property or that information, that is the class that should manage it.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    The problem:

    class 1 (stored data from user input - configuration)
    class 2 (multiple instances - do the work - more at the same time)
    class 3 (preforms the action/function)

    So I have a data in class 1.. class 2 works, and needs class 3 to do some action(s) to process data that is stored in class 1..

    There would be no problem if variables from class 1 would be global (I could also use a namespace) - class 1 is made to read all the variables/configuration of the program and has functions to preform that task..

    I have somehow to get data from class 1 to class 3 (not copy it because it wont need the whole data), no idea what would be the best way.. I'm really confused..

    It (class 3) just needs to get lets say a vector of strings from the class 1, but that is hard to do because its being made and called from class 2.. So I would have to pass data to the class 2 and then to class 3 (which is stupid)...

    I'm really not sure what to do, and want to do it on professional way (dont want to write crappy programs)...

    Any ideas for better design?

    Thanks for help again

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    When you've made some classes, the point is that you can't directly manipulate with any variables when outside the class, you need to do it through functions. Though some variables are easier to use directly.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    So I could make the next solutions:

    1) global function get_data
    2) make namespace with data (not class)
    3) pass the data to class 2 -> pass data to class 3
    4) different design?

    What would be the best approach?

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by l2u
    The problem:

    class 1 (stored data from user input - configuration)
    class 2 (multiple instances - do the work - more at the same time)
    class 3 (preforms the action/function)

    So I have a data in class 1.. class 2 works, and needs class 3 to do some action(s) to process data that is stored in class 1..

    There would be no problem if variables from class 1 would be global (I could also use a namespace) - class 1 is made to read all the variables/configuration of the program and has functions to preform that task..

    I have somehow to get data from class 1 to class 3 (not copy it because it wont need the whole data), no idea what would be the best way.. I'm really confused..

    It (class 3) just needs to get lets say a vector of strings from the class 1, but that is hard to do because its being made and called from class 2.. So I would have to pass data to the class 2 and then to class 3 (which is stupid)...

    I'm really not sure what to do, and want to do it on professional way (dont want to write crappy programs)...

    Any ideas for better design?

    Thanks for help again
    You could:
    * Make class1 do nothing but read/save/manipulate settings

    * Have class2 contain a static private variable of type class1; class2 would become your main class

    * Have class2's instances pass the relevant data to class3 objects as needed.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's a good Object Oriented Strategy for OpenGL/Graphics
    By indigo0086 in forum Game Programming
    Replies: 9
    Last Post: 04-03-2007, 06:27 PM
  2. object oriented C
    By FlatLost in forum C Programming
    Replies: 4
    Last Post: 11-08-2005, 06:22 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM