tags:

views:

77

answers:

4

This applies to several cases in my application:

I have 3 or 4 functions that belong together, one is a starting function that creates and frees the required memory structures and calls the other functions as appropriate. The other functions also call themselves repeatedly. Only the starting functions is called from outside, and only once or not at all per application-run.

Currently, I pass pointers to the memory structures from the starting function as function arguments, but the argument list is getting quite long in some cases.

Is there any argument against creating classes for all these cases and making the pointers to the memory structures members?

+3  A: 

Definitely go for a class here. That's what objects and classes are designed for.

dark_charlie
Depends; it is, if they can be considered an "object". Otherwise, not so much.
You
Sorry, could you elaborate what you mean by "object".
Frank
Object is an instance of a class. A class/object contains data and methods that work with that data (and only such methods, if there were methods that are unrelated to the data of a class/object, it's violating of the OOP principle).
dark_charlie
"Object" in the OOP sense of the word. Can you have one or many of it (e.g. a car, a house)? Can you perform operations on it (methods)? It's an object. If you're just collecting a bunch of functions, it's not an object.
You
No, in fact, it is not really an object, I am collecting a bunch of functions that logically belong together and use the same data. I think about using a class to simplify the code.
Frank
The more I think about, with some imagination, you may consider it as an object :)
Frank
A: 

It sounds to me like these functions belong in a class - with the internal functions private or protected - likewise with the members.

Will A
A: 

Yes, relate them all within a class.

This will also give you a cleaner code which might help you minimize the functions' arguments lists.

In simple words an object can be anything you decide. Say a person. This is an object which I'll decide to define as a class if I'll write a program that needs to keep information regarding people.
There are much better explanations than this, just google/wikipedia it.

Poni
+2  A: 

It seems to be a quite typical use case for classes: Just add the "memory structure" as protected member of the class and initiliaze it in the constructor.

The member functions (aka "method") than can work on the data.

If you have different, but similiar use cases, you may also make use of subclassing, so you create a base class with default implementation and create some derived class that overwrites some of the methods with an own implementation.

But note, that you could also use other members varibales to set the behaviour at runtime (e.g. a bool that is used to toggle on or off a specific behaviour).

Your question is too abstract to see what is the best solution for your case. Remember, often there are a lot of solutions - and so there is more than one good solution.

IanH