predefined object "models"?

hello everybody.
I'd like some suggestions on object creation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Styles{
  int id;
  int right_margin;
  string name;
  //etc
  Style(int, int, string);
};
class Texts{
  string text;
  //etc
  Texts(string, Style)
};

int main(){
  Style style0(0, 4, "style_zero");
  Style style1(1, 10, "style_one");
  Style style2(2, 2, "style_two");
  Texts text("Hello!", style1);
  return 0;
}


well, i want to have some dozens of different Styles to be chosen from, everytime i create a new text. but everytime i use the program, only one of them will be picked up. so all the others will be useless, as will the chosen style after the Text construction has ended. yet all these style objects consume memory...

so my question is: what would be the better approach to solve this? this is what i could think...
1) create all the objects anyway, regardless of memory consumption (which I'm trying to avoid)
2) create all "objects" as macros (is that even possible?) so they will not really exist in memory. then i just put the macro i want in the text creation.
3) keep all the possible styles in an external text file, to be readen only when necessary. i like this, but not the idea of people being able to edit the files.

does databases fit here? i have absolutely no experience with it, so i do not know if it's a good offline option.

anyway, i'd like to know if there are any alternatives. thanks in advance!
Last edited on
I'm not entirely sure what you are asking here. From what I understand, you have a bunch of options, but only one of them will ever be used in the program at a time. Do you select the style at run time or compile time?

Assuming you select it at run time, a file is probably the best option. There is no real benefit to making the file uneditable - in fact, there might be benefits to making the files editable, just so users get the additional customization options, if they want. Most of the time, if you just call the file 'style1.dat' or the like, people will ignore them as things not explicitly there for editing.

On another note, macros are processed at compile time, so they couldn't be used like how I think you are asking. The same goes for using templates. Also, storing them in memory even if you don't use them (as long as you put a boundary on their lifetime) is quite often OK - its not often that programs are memory constrained any more.
class Styles >> class Style

I don't much sure about the way you're using it.
How will you assign the style, such as style1 how you assign that's (1, 10, "style_one");? If you'll assign all while you compile, place it in your code (maybe separated file using #include ) or in file does just a little different(just in bytes, I think) but you don't have to write file-command if you placed it in your code.

But if the style can be dynamic-assign when program runs, using file will be okay.
ok, got it.
yes, styles would be selected at run time.and i made a confusion with the macros, they really would not be possible at all...

i could also create an separated function that stores all the possible objects inside it, so i can at least have more control on their lifetime. but i think i'll just make it readable from an external file then.

thanks for the answers!


ps: "Styles" and "Style" was a typo :P
Topic archived. No new replies allowed.