|
-
Oct 4th, 2001, 11:41 PM
#1
Thread Starter
Hyperactive Member
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.
-
Oct 4th, 2001, 11:49 PM
#2
Thread Starter
Hyperactive Member
would ther be some way of creating let/get properties?
thanks
Amon Ra
The Power of Learning.
-
Oct 5th, 2001, 01:11 AM
#3
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
-
Oct 5th, 2001, 06:20 AM
#4
transcendental analytic
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.
-
Oct 5th, 2001, 11:18 AM
#5
Thread Starter
Hyperactive Member
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.
-
Oct 5th, 2001, 11:35 AM
#6
Monday Morning Lunatic
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
-
Oct 5th, 2001, 11:41 AM
#7
Thread Starter
Hyperactive Member
so they should just be public?
Amon Ra
The Power of Learning.
-
Oct 5th, 2001, 11:46 AM
#8
Monday Morning Lunatic
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
-
Oct 5th, 2001, 12:24 PM
#9
transcendental analytic
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.
-
Oct 5th, 2001, 02:08 PM
#10
Monday Morning Lunatic
-
Oct 5th, 2001, 03:13 PM
#11
Thread Starter
Hyperactive Member
so kedaman, should i keep my accessors?
Amon Ra
The Power of Learning.
-
Oct 5th, 2001, 03:23 PM
#12
Thread Starter
Hyperactive Member
i would have a very large amount of code for these accessors if i do keep them..
Amon Ra
The Power of Learning.
-
Oct 5th, 2001, 03:27 PM
#13
Monday Morning Lunatic
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
-
Oct 5th, 2001, 03:52 PM
#14
Thread Starter
Hyperactive Member
i have quite a few members though..like 10..
Amon Ra
The Power of Learning.
-
Oct 5th, 2001, 03:54 PM
#15
Monday Morning Lunatic
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
-
Oct 5th, 2001, 04:10 PM
#16
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|