pass info to a class or use class members

Hi, some confusion in learning this. I have a custom class with a public function which needs some string to be passed in order to process them. AFAIU these information could be easily passed as arguments:
 
myClass->function(stringA, stringB);


but I also see I could declare new string objects as members of the called class and before actually calling the function define these members from the caller function.

1
2
3
4
5
6
myClass->member1 = stringA
myClass->member2 = stringB
myClass->function();

calledClass::function();
//some some stuff with member1 and member2 


To me the second option is feasible but not suggested, the reason I understand is about the dependencies I want to avoid between the function and its members, the former won't simply work if the latter are not defined. Does it make sense?

I wonder if there is any other comments on this so that I clarify it to myself.
You can use getters and setters. I own the project that's at sourceforge.net/p/gucci, and you can download it and look over the class that I have in there.
Understand and it is interesting, thanks. So in theory there should never be the need to use a public member but only functions to access to them with getters and setters method. Is it true? Is there any example of when to use public members?

Also, what about dependencies instead? Still, there is the need for the user to set the value to this members before calling the function which actually use them (and does not work if the value is not passed)
I don't get it.

Why would you make members private but then have all of them accessible by getters and setters? What's the point?
Last edited on
Unlikely I would need to make them public which is not good as I could not longer take advantage of abstraction for the user.
But doesn't using getters and setters go against that?

What's the difference between

1. declaring every member variable public
2. declaring every member variable private but having a get and set for each of them.

I'm sure the get/set method will likely make the code easier to maintain and modify but seems like if you need to use get/set for a lot of member variables there's a deeper problem with the class set up.

Not that I have much experience in working with classes, just wrote my first abstract base class the other day but I wrote an asteroids game over the past few days (to practice using ABCs) that has 6 classes I think and not a single get/set function was needed.
Last edited on
I am not the authority here to explain this, but meanwhile someone else get in I can tell you AFAIU it is mainly for abstraction, for example in case in the future you want to change the type of member for some reason, say a string to a int, the user which is using your class it is not affected, which is a basic with OOP I think...
Getters and setters are for making the changing of members of a class more intentional. It forces you to think about what you're doing, and do it on purpose.
¿How is hero.name() more intentional than hero.name ?
Or hero.set{health,attack,defense,magic,...}({values}) against hero.level_up();


Off-topic comments about your code:
deleteChar(characters, it); in that call it has no meaningful info, and the info collected is not reused. ¿why is it an argument?

¿what is the purpose of the Character class? It is too coupled with Character_Impl and adds nothing
hero.setName([string literal]); vs name = [string literal]; forces you to separate the use of a class against what could be accidental modification of a member, especially when applied to potentially careless uses of namespaces.

As far as the iterator, I think that was designed to use the same iterator in every function. (I'm not the sole author of this code).

The Character class is incomplete. It's designed to represent the abstraction between a GUI component and the implementation of the Character_Impl class. It will eventually disappear as we change the Character_Impl class object into a shared object.
I don't understand. ¿How could you accidentally hero.name = [string literal]; ?
You never know what people are going to do to your code if you're working in teams, and in my opinion it's a bad idea to make class object members public without a very good reason. I tend to default almost all of my members, as well as any methods that don't require outside information, to protected, and then use getter and setter functions to access or modify the information.

Like I said, it forces the people using your code (not necessarily you) to be more cognizant of what they're doing, and what changes are being made.
I think we all agree it's a bad idea to make the members public, but why instead of using get/set why not just have functions inside the class that perform any calc for you.

For example, could all this

1
2
3
4
5
for (it = characters.begin(); it != characters.end(); ++it)
    {
        mySaveFile << (*it)->getName() << " " << (*it)->getVoid() << " " << (*it)->getStam() << " " << (*it)->getWill() << " " << (*it)->getRef() << " ";
        mySaveFile << (*it)->getInt() << " " << (*it)->getStr() << " " << (*it)->getPercp() << " " << (*it)->getAgil() << " " << (*it)->getAware() << " ";
    }


not be replaced by a member function that takes a reference to a text file which is defined within the class to save date. That means all those gets (except getName which you have used elsewhere) are no longer needed.
@Muckle:

I intend to gut that code and overload the stream (<<) operator and just use mySaveFile << *it;

I just haven't gotten around to it.
The front end of the program hasn't even begun to be worked on, what's there right now is only there so that the other two people that contribute to the code actually have a way to verify the data that's inside the classes. We're planning on using Qt to actually build a front end, and control objects on the back end with .so or .dll files (depending on platform), so not much of the implementation is actually there.

The stats are going to need to be accessed for display, and for things like initiating combat or making changes to temporarily retarded or enhanced individual stats. The whole system is undergoing an overhaul right now, I just put it up there so that the three of us that are participating can each have access to what we've got right now, and then make changes as needed.
Topic archived. No new replies allowed.