C
cppaddict
Let's say you want to implement a Dictionary class, which contains a
vector of DictionaryEntry. Assume each DictionaryEntry has two
members, a word and a definition.
Now assume your program needs to create a Dictionary *object* to be
populated with values that come from a text file with a format like
this:
<dict.txt>
APPLE
a fruit
ANT
an insect
....etc...
</dict.txt>
Clearly it would not be hard to write a parser that went through the
text file and populated the object. This, however, makes the program
depenedent on an uncompiled text file, which could be a problem if,
eg, the words and definitions were all top secret.
One solution which I don't like is to write a program that converts
the text file into a .cpp file which, in turn, defines the dictionary
object you'll need and populates it. The result of the conversion
might look like:
<dict.cpp>
Dictionary SECRET_DICTIONARY;
DictionaryEntry E1("APPLE","a fruit");
SECRET_DICTIONARY.addEntry(E1);
DictionaryEntry E2("ANT","an insect");
SECRET_DICTIONARY.addEntry(E2);
....etc...
</dict.cpp>
Then your client program could #include dict.cpp and use
SECRET_DICTIONARY as needed. Of course, this requires you to:
1. Write, compile, and run the conversion program to produce dict.cpp
2. #include dict.cpp in your program, and then compile that.
Thus a two step compilation process. Is there a better way to handle
this situation?
Thanks for any suggestions,
cpp
vector of DictionaryEntry. Assume each DictionaryEntry has two
members, a word and a definition.
Now assume your program needs to create a Dictionary *object* to be
populated with values that come from a text file with a format like
this:
<dict.txt>
APPLE
a fruit
ANT
an insect
....etc...
</dict.txt>
Clearly it would not be hard to write a parser that went through the
text file and populated the object. This, however, makes the program
depenedent on an uncompiled text file, which could be a problem if,
eg, the words and definitions were all top secret.
One solution which I don't like is to write a program that converts
the text file into a .cpp file which, in turn, defines the dictionary
object you'll need and populates it. The result of the conversion
might look like:
<dict.cpp>
Dictionary SECRET_DICTIONARY;
DictionaryEntry E1("APPLE","a fruit");
SECRET_DICTIONARY.addEntry(E1);
DictionaryEntry E2("ANT","an insect");
SECRET_DICTIONARY.addEntry(E2);
....etc...
</dict.cpp>
Then your client program could #include dict.cpp and use
SECRET_DICTIONARY as needed. Of course, this requires you to:
1. Write, compile, and run the conversion program to produce dict.cpp
2. #include dict.cpp in your program, and then compile that.
Thus a two step compilation process. Is there a better way to handle
this situation?
Thanks for any suggestions,
cpp