Results 1 to 16 of 16

Thread: class

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    class

    i have a class with quite a few members..i was wondering if i should create "reader/writer" functions, or simply make the member variables public? i thought it would be more organized to have the accessors, but i am not sure..any advices?
    thanks
    Amon Ra
    The Power of Learning.

  2. #2

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    would ther be some way of creating let/get properties?
    thanks
    Amon Ra
    The Power of Learning.

  3. #3
    Destined Soul
    Guest
    Originally posted by Amon Ra
    would ther be some way of creating let/get properties?
    thanks
    Not that I'm aware of myself. As far as I know, if you call: "object.x = 3.0;" I think it will look for a non-private member variable (double x

    I really wish that they would put this in, though. When I first learned that VB had such a function, I was incredibly surprised. It sure beats "double GetX()" and "void SetX(double Val)"

    Oh, as for the read/write functions, it's usually a good habit. My rule is that if I only just set/check the value of it without having to do any math to it. Otherwise, I usually put it into a function. That way if any other math is always done to the variable x, you know that it's the function "SetX(..)" that's going wrong and not the code that calls it.

    Well, you get the idea. I guess the proper way to think of it is to make the class completely idiot-proof, even if you're the one using that class. Ever make a textbox that inputs ONLY numbers, ensuring that if anything else is entered, an error occurs? If so, you should have an idea...

    Anyhow, as usual, I'm starting to babble...

    Destined

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Property Get&Let/Set produce the illusion of direct access to a variable. Mistaken this, a big source of bugs is when you try to modify a variable that has been retrieved with Property Get.

    Encapsulation is a important issue, depending on how much exposed your class is meaning you estimate the range of use of it, the more reason there is to keep proper encapsulation, protect data and defy assertions. Ideally you would follow this rule at all cost but in practice you will be producing layers and loose the encapsulation and protection within certain bounds. At times you also have to make compromises between runtime speed and the OOP paradigm, but that you shouldn't do without being certain about the results. Regularily you compromise between OOP and design time.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    yea basically my class is a bit lik ethis:

    Code:
    class CLASS      {
       private:
            int a;
            long c; 
            char *b;
      
       public:
            int geta() {return a;}
            void seta(int val) {a=val;}
           ....
    }
    you see nothing special is happening to these variables, but i thought it still looks more organized, and clean...it is quite a bit longer, but i'll keep it that way anyways..
    thanks for the advices =)
    Amon Ra
    The Power of Learning.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    But what's the point of doing that? The idea of hiding your member variables is to hide the implementation details - for example your class's users shouldn't need to know that you use a float, a string, and an int* (for example), so making them private and using get/set functions for them all is worthless.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    so they should just be public?
    Amon Ra
    The Power of Learning.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, if all you're going to do is make get and set functions for them all, then basically yes

    However...if you need to verify the new values then you can use get/set - it's not bad as such to use them, but it *is* bad to use them for everything, making a data-access interface rather than an "action" interface.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Aha, parksie here again, trying to sneak in and undermine encapsulation! Don't listen to him, because he talks bullcrap

    Since you are not experienced with object orientation in general, you should practice on it by using get/set methods. In case you are working with a small project, or just playing around or testing something, then it's ok, but by practicing encapsulation you learn the meaning of it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Do I?

    This could get amusing...nobody can actually win this discussion so I'm gonna be militant about my opinion, and I'd expect no less from Mr. Sugii

    As far as practice goes, that's ok, but OOP design recommendations say that you shouldn't expose your implementation in a way that might make your users dependent on that implementation. And if that sounds heavy and incredibly complicated, that's because it is but then again this is C++ - sanity when you've finished learning it (if you ever do) is not guaranteed.

    I mean, just look at what happened to me
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    so kedaman, should i keep my accessors?
    Amon Ra
    The Power of Learning.

  12. #12

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i would have a very large amount of code for these accessors if i do keep them..
    Amon Ra
    The Power of Learning.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Inlining helps here:
    Code:
    class X {
    public:
        inline int GetVar() { return m_iVar; }
        inline void SetVar(int iVar) { m_iVar = iVar; }
    
    protected:
        int m_iVar;
    };
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i have quite a few members though..like 10..
    Amon Ra
    The Power of Learning.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Sorry, no alternative to it - you have to code all the functions up.

    Actually, if you're using MSVC++ then you can use a __declspec on it, but that's nonstandard.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    ok thanks..btw i do have msvc++, but it wont hurt me to write a few more lines of code...=)
    thanks
    Amon Ra
    The Power of Learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width