Thread: Array of classes?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Array of classes?

    I have a question regarding a good way to handling a large number of similar classes.

    I have a number of classes representing operations to perform on my data class. Now the data has to meet specific conditions specific to each operation for the operation to applicable. I need to be able to check for each operation if its applicable and to do this in each class I have a function which given a data set returns whether or not the operation can be used on that data set. Each of these operations shares a parent class.

    I know lots of different ways of handling this but I haven't found one that isn't messy. On one hand I can create an instance of each operation and have an array of them and iterate through it checking each class if its applicable. This is easy in the sense that I just loop through checking. This is a messy solution in the sense I need to maintain an instance of each operation (potentially a lot of them) and when I use an instance to mutate data I then need to create a new unused instance and place it in my array. On the other hand I can have the function to check if its applicable be static. This way I don't need to have instances of each class. I do however have to have a line in my code checking each and every single class and then again when I want to apply an operation I need to lookup the class from a string and then instantiate it. This creates two very large messy functions that are just lookup functions essentially.

    I'm looking for a middle ground. If I could have an array of functions it might work but I haven't figured out how to do that (I know how to make a function pointer.. but G++ gives an error if I create an array of them). Again if I could have an array of classes (not instances, the actual class to instantiate instances from) it would work even better.

    If anyone has any hints I'd love to hear them, thanks.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    First thought - why are these operations not members of your data class in the first place?

    EDIT: OK, I just reread and see that some are not applicable. Google the Chain of Responsibility design pattern.
    Last edited by medievalelks; 03-22-2009 at 11:33 AM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Operations might not exactly the correct word. I was trying to abstract the problem a bit. The operations are actually logical rules that manipulate a logical statement (for P and Q implies R). The data is a logical statement of formulas, terms, constants, variables, functions and predicates. Each rules takes one logical statement and creates a new logical statement slightly different from the old one, leaving the old on in tact. The rules are numerous and some are kind of long. The rules also form a tree like structure in their own right. Combining both of these classes together would be a mess unfortunately.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Char array and classes
    By Heineken in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2005, 12:32 PM
  4. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  5. array of classes in C++
    By stanleyw in forum C++ Programming
    Replies: 2
    Last Post: 05-29-2002, 08:20 PM