1

I am developing a C++ application where I need to create X amount of classes. Let's say: class AAA, BBB and CCC. Each class corresponds to one type of object which I would need to process. On the other hand, I have an input file of CVS type. One example of that input file looks like this one:

AAA,3432443,433434,11111,45678
AAA,8778776,786698,22222,86881
BBB,4452332,112234,34543,87734

So, I need to parse that input file, and according to the first element of the line (AAA, BBB, CCC), I need to create the corresponding object which will store the other values found in the line. BUT, I cannot "embedded" in the application the possible classes (AAA, BBB, CCC) in order to use a chain of conditionals, like: if (token == "AAA") then AAA aaa = new AAA (params); Instead:

  • The application must remain independent of the existing classes, in such way that in the future we can add classes DDD, EEE, ... ZZZ, without modifying the module which creates the objects (in case of using conditionals, I should add, for instance: if (token == "DDD") then DDD ddd = new DDD(params). This is forbidden.
  • I have created a configuration file which provides the possible class types in the current execution: AAA BBB CCC ... In this way, I can loop over this class types and compare against the ones read from the first element in the input file lines. But I am stuck in the problem of how I can create an object of an unknown class. Something like this: retrieved_type_from_input_file object = new retrieved_type_from_input_file(params); where "retrieved_type_from_input_file" is a variable which contains the effective type of class I should create: AAA ... ZZZ
  • I am thinking in generic programming. Something like: T object = new T(params); But even though I can code this inside a class; in the implementation I need the real name of the class.
  • I am thinking also in the implementation of some kind of Factory pattern, creating some kind of abstract parent class ("Thing") from which everyone would be inheriting ( class AAA: public Thing). But all the models which I find, make explicit reference to the name of the class, in some place of the code, at the moment of creating the object.

Any help please? Thanks a lot in advance!!

Javier Martín
  • 2,537
  • 10
  • 15
Karol Baum
  • 63
  • 7

1 Answers1

0

i am guessing, you are in need of code generator :

Step 1. Write a program that generates classes in .h, .cpp files based on your configuration file.

you can alse create/declare objects of the classes in this step

Step 2. Make use of generated classes , to build an exe file.

This is my rough idea, and looking for other possibilites from others.

vsmph
  • 108
  • 1
  • 11
  • Hi, did you got chance to implement, how you did, and little curious. Are you used factory or used code generator or something else. – vsmph Oct 15 '16 at 02:08