0

I’m designing a user interface in C++. I have a class called UI which is the parent of the UI. My program has single instance of the UI class which contains UI wide data like colors and fonts etc. as well as all of the control objects. This object also has a render function which renders the entire UI by calling the render functions of every child object. The render functions of the child objects need colors and fonts to render the controls.

So my question is how can a child object access the color and font variables of the parent object?

Kestis
  • 163
  • 1
  • 6
  • 4
    Please show your code and explain what problem you are having with it. See also [mcve] – UnholySheep Jan 16 '18 at 09:01
  • You can either explicitly supply a reference to parent class to each child object (probably as a constructor parameter) or you can provide a global access point to get a common instance of parent object (e.i. make a singleton parent object). – user7860670 Jan 16 '18 at 09:03
  • Are you writing your own UI system? For practice? If you just want to create a GUI for an application, I strongly recommend using a GUI library like wxWidgets, Qt, MFC or similar. – Hafnernuss Jan 16 '18 at 09:08
  • 1
    Pass the information as parameters to the render functions. – molbdnilo Jan 16 '18 at 09:26
  • I’m doing this mostly for practice. I’m making a quite specialized program as a hobby and a custom GUI works best for it. – Kestis Jan 16 '18 at 09:37
  • Currently the colors and fonts are defined inside the render functions of each control to test their operation. Now I’m focusing on the appearance and need a way to centralize the color and font definition. – Kestis Jan 16 '18 at 09:42
  • What on earth is this obsession with singletons? (1 comment and 1 deleted answer) We're talking simple inheritance where OP hasn't bothered to post MCVE to show the actual problem and misunderstanding. There's no need to mislead OP to use a generally dodgy design pattern with absolutely ***zero*** justification! @Kestis you seem to be struggling with fundamentals. If you don't [edit] your question to a show a [mcve], how do you expect anyone to help you? We cannot read your mind to understand what you're misunderstanding. – Disillusioned Jan 16 '18 at 13:18
  • @VTT How on earth do you expect passing a reference to parent to provide any more access to attributes than ***is already available*** due to simple inheritance? OP is stuck with fundamentals. We need to see code to see where this sticking point is. It doesn't help to give bad advice without any idea of what OP's problem is. – Disillusioned Jan 16 '18 at 20:28
  • @CraigYoung I assume that there is no inheritance in this example. Even though there is no code Op clearly stated that there is "single instance of the UI class" and "all of the control objects" which implies that control objects are owned by UI class, not inherit from it. Also "single instance of the UI class" suggests that Op already has a singleton regardless of its form. – user7860670 Jan 16 '18 at 20:37
  • @VTT The question in title and restated in main text is: "So my question is how can a **child object access** the color and font variables of the **parent object**?" This contradicts your assumption. Perhaps OP is confused about what terminology to use, there's no way we can guess. At best the question is unclear. – Disillusioned Jan 16 '18 at 22:11

0 Answers0