Think it´s alot of code for almoast nothing!!
Quote:
Originally posted by kitten
Accessing class' variables via set and get functions is a common way to do it. And there is no other good way if the variables are private (like they should always be). So examples of accessor functions for your class would be:
Code:
const char* const GetForeName(void) const
{ return foreName; }
void SetForeName(const char* const NewName)
{ strcpy(NewName, foreName); }
int GetAge(void) const
{ return age; }
Thanks for you´re answer, But is this really the way you would do it? If you have lots of variables, it will be awful many methods, for almoast nothing!
I know that this is the secure way of doing it, but if you make the variables public, you could do without no method what so ever. Eaven if it is a not so god way of doing it.
I do want to do it the C++ way, but I think that struct is so much easier, less work..
Possible other way of handling classes..?
Hmmm..... now, I may be completely wrong about this (so please don't leap on me for suggesting it :) ).
I think one way of doing this which prevents writing a load of code which you don't necessarily *need* to write would be to put together a sort of base class which houses the get and set routines (though not really base, if you see what i mean).
It depends on what you want to use it for of course, but this kind of thing may be useful... I'm sure there are many ways of extending the base class (VarClass in this little bit I wrote) to hold multiple variable types (or if you're lazy you could just write one for each :) ). The MainClass doesn't really have to be there obviousy but I kind of guessed that's what you were getting at..
If anyone would like to suggest a different way of doing this kind of thing (like a default method for classes... is there one?? then you could analyse the call and figure out the variable to operate on... hmmm I dunno) that would be interesting...
- Almosthere
#include <iostream.h>
class VarClass {
protected :
int MyVar;
public :
VarClass() { };
VarClass(int n) { MyVar = n; };
int get() { return(MyVar); };
void set(int n) { MyVar = n; }
};
class MainClass {
public :
VarClass *Teete;
MainClass() {
Teete = new VarClass;
}
};
void main(void) {
MainClass HooHoo;
HooHoo.Teete->set(10);
cout << "Teete is... " << HooHoo.Teete->get() << endl;
}