-1

This seems to be a simple problem to state, but I am unable to understand what the solution is. I have a screen class where the objects are basically x and y coordinates, font color, etc. All is working well, I can instantiate and use these objects.

Sometimes I need to change the color of the string that is being written to the screen. This is more of a "system call" that once done, sets all strings written after that. So each time I write to the screen, I am forced to first "set the text color". Or do I? I have a method, setValue: objA->setValue("1234", RED);

When the method gets this call, it will set the color of the screen write. Let's say objA just set the color RED. If another object needs to write to the screen, and it's the same color, I don't want to write that to the screen again as to reduce the number of writes to the screen. So, based on the last object's color, I want to update the screen text color (or not) In C, this of course would be the most evil static global. How can I do something similar in C++?

Thanks...

Wtower
  • 18,848
  • 11
  • 103
  • 80
user10326
  • 31
  • 4
  • Hi,Thanks for answering ! "setValue" is an overloaded function that can either set a specific graphic object, or set text to an x,y location. Your suggestion of class static variable works perfectly well. Thanks again! – user10326 Jun 27 '16 at 21:35
  • Actually, since I cannot dynamically allocate memory (using mem model 2 in FreeRTOS) I am using "new" to create objects, which should exempt the need to create them as static. Declaring the variable as private and setting/testing it works well. – user10326 Jun 27 '16 at 21:46
  • Welcome to Stack Overflow! I edited your question to format the inline code sample so that it renders properly - please see the editing help for more information on formatting. Please edit in to provide additional detail that's necessary to identify the specific problem. Good luck! – Wtower Jun 30 '16 at 04:37

1 Answers1

0

I'm not completely sure what you're going for here: I.E., if the objects you're using are meant to represent the screen or a bit of text within the screen. I'll assume the latter.

It sounds like you just want a place to squirrel away your color value, for subsequent calls that don't specify the color value. You should probably look into class static members. In the most simple-minded case, you'd simply have a private static class member of your "color" type and have your "setValue()" function simply update that variable.

(BTW,If I'm reading your question correctly, "setValue" should really be called something like "write text". Maybe I misinterpreted, though. It would also be sensible to split off the "set color" for your class into a public static member function. Not a correctness thing, just a clarity thing.)

D Hydar
  • 502
  • 2
  • 8
  • I'm not sure if `static` will be needed at all. – πάντα ῥεῖ Jun 27 '16 at 21:11
  • Yeah, it depends if the call is a per-screen or per-object-in-screen; the static would be needed if > 1 object per screen, since it would be shared. If this is just a "screen" object, then the color would just be a plain old member. – D Hydar Jun 27 '16 at 21:24