Accessing/working with member objects of parent classes

This is a discussion on Accessing/working with member objects of parent classes within the C++ Programming forums, part of the General Programming Boards category; *I know this is not the Windows/Linux forum, but this question pertains more to C++ than GUI concepts* What is ...

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question Accessing/working with member objects of parent classes

    *I know this is not the Windows/Linux forum, but this question pertains more to C++ than GUI concepts*

    What is the best way to handle it when one class needs to access the members of another class, and those members are objects? For example, suppose I have a main window. That window has child controls (objects), and when events happen with those child controls, menu items in the main window's menu bar needs to be enabled or disabled.

    What is the best way to access these parent objects and work with their properties from the child objects? Should I go against the rules and make the controls public? Should I have Get() methods? Friend classes? A better way/best way?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I might be misreading your question, but I don't think controls inside a window should be children of the window. That's a has-a relationship, not is-a. A window has-a button; a window is not a button. I'd make the buttons and other controls part of the window class. In that case, your access problem would be solved, but I'm sure there's more than one way to go out about this.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. One solution is a boolean variable in the window class. Another solution is to determine the status of the controls via, for example, IsWindowEnabled().

    Kuphryn

  4. #4
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Alright. Suppose my application has a dialog, set as a child of the main window. When the ok button is clicked, a number of objects in the main window need to be updated. Text boxes might need to have their values changed, menus might need to be enabled/disabled, a side panel window ("sash window" - the one with a draggable border) has its own controls that need to be updated.

    Here's a diagram of what the heirarchy might look like:

    main window
    dialog
    controls within this child dialog
    side window
    controls within this side window

    So what if the child dialog needs to access a method in the side window's class? Should I say something like this (from within the dialog class):

    GetParent()->GetSideWindow()->DoMethod();

  5. #5
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Can anyone see anything wrong with a friend class?

    I know that with GUIs, child and parent relationships are not really child and parent relationships - at least not in the inheritance/polymorphism respect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  2. Replies: 5
    Last Post: 07-25-2008, 05:37 AM
  3. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 09:02 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Modifying CDocument Member Objects :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-12-2002, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21