A class pointer to the class I come from

This is a discussion on A class pointer to the class I come from within the C++ Programming forums, part of the General Programming Boards category; I have the following class: [tag] Code: class myGui { public: MyWidget w; swmArray s; GearWidget g; quitDialog q; int ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Question A class pointer to the class I come from

    I have the following class:
    [tag]
    Code:
    class myGui
    {
    public: 
      MyWidget w;					
      swmArray s;
      GearWidget g;
      quitDialog q;          
      int browseNum;
      DirectoryWindow mw[20];
    public:
      myGui(void);
    };
    [/tag]
    I want the members (which are other classes I made) to have a pointer to the instance of myGui to which they are a member.

    For example in the class MyWidget I wish to have a member "myGui *mypointer;" thats points to the instance of myGui that contains my instance of MyWidget.

    You see the problem. They will #include each other. How do I go about this?

    -- Paul
    Last edited by Lumber606; 07-22-2003 at 02:35 PM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    If you need to, put class Widget; or whatever other class you have in front of everything so that you can use pointers to any class before the classes are actually defined.

    ie:
    Code:
    class A;
    class B {
      A* func();
    };
    class A {
      //...
    };
    If you want your member objects to contain a pointer to the object they are a member of, use a constructor. Pass 'this' to each object that way. ie:

    Code:
    class Widget { //...
    private:
      Widget();  //this makes sure that the only possible constructor is the bottom one
    // You may want to declare a copy constructor to be safe
      Container* parent;
    public:
      Widget(const Container* b) : parent(b) {}
    };
    class Container {
    public:
      Widget object;
      C object2;  //some other widget
      Container() : object(this), object2(this) {}
    }
    Last edited by ygfperson; 07-22-2003 at 04:53 PM.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    I'm not sure but I think some of the A's and B's are mixed up the second example. Are they?

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by Lumber606
    I'm not sure but I think some of the A's and B's are mixed up the second example. Are they?
    Yep... I fixed them now. Thanks for checking the code.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> If you need to, put class Widget; or whatever other class you have in front of everything so that you can use pointers to any class before the classes are actually defined.

    Aka 'forward declarations'.

    Note, it could be a pointer or a reference with just the forward declaration.
    Last edited by Zach L.; 07-22-2003 at 06:48 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Initializing a pointer in a class.
    By gpr1me in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2006, 02:05 PM
  3. class passing a pointer to self to other class
    By daioyayubi in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2005, 09:25 AM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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