Classes, pointer to itself to access from a child class

This is a discussion on Classes, pointer to itself to access from a child class within the C++ Programming forums, part of the General Programming Boards category; I have a main class with some child classes inside, I need to be able to access other child classes ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    Question Classes, pointer to itself to access from a child class

    I have a main class with some child classes inside, I need to be able to access other child classes from a child one or members from the main class like the dosomething() function in the example.
    How could I initialise the child classes when the main class is created passing an argument to each one pointing their parent class?

    Thanks in advance
    Code:
    class subExample {
             mainExample *classParent;
             int i,j;
             int dosomething() {
                    i = i+j+24;
                    parent->doAnotherThing();
             }
             subExample(mainExample *parent) {
                    classParent = parent;
             }
    };
    
    class mainExample {
            subExample childClass;
    
            int doAnotherThing() {
                     etc
            }
    };
    Last edited by lautarox; 09-22-2010 at 01:36 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    What I think you're asking for is this. If this doesn't answer your question, please elaborate.
    Code:
    class SmallComponent {
    private:
        BigThing *outer;
    public:
        SmallComponent(BigThing *outer) : outer(outer) {}
    };
    
    class BigThing {
    private:
        SmallComponent small;
    public:
        BigThing() : small(this) {}
    };
    By the way, that's a bidirectional dependency (each class is depending on the other), which is very tight coupling. In short, it's not a good idea. Maybe you can explain in more detail what you're trying to do, and why.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    The parent could have a manager and the child function can return a value that tells the parent manager if it needs to run another child function or one if its own (parent) functions next. Changing the parent inside the child class sounds dangerous.

    ex:

    Code:
    class subExample {
          private:
             int i,j;
          public:
             int dosomething();
    };
    
    int subExample::dosomething()
    {
      i = i+j+24;
      return i;
    }
    
    class mainExample {
          private:
            subExample childClass;
          public:
            void Manager();
            int doAnotherThing(int i);
    };
    
    int mainExample::doAnotherThing(int i)
    {
      i += 10;
      return i;
    }
    
    void mainExample::Manager()
    {
      int temp;
      temp = childClass.dosomething();
      if(temp == 24)
        temp = doAnotherThing(temp);
    }
    @dwks
    My compiler wouldn't even compile that example because it gets to class smallcomponent and doesn't know what a bigthing is yet because it hasn't been declared. I wasn't aware there was a way to use two classes not dependant on the order they were declared.

  4. #4
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    That was what I meant, I'm doing a file transferring project in order to archive some experience socket programming, the program has an individual class for each connection, and each connection has some classes, one storing the window class for that connection (Gtk) to modify visual values and a class to control the connection.
    What would be the best option to do that?

    Code:
    class SmallComponent {
    private:
        BigThing *outer;
    public:
        SmallComponent(BigThing *outer) : " outer(outer) {}" -> Here, why are you doing that?
    };

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by Lesshardtofind View Post
    @dwks
    My compiler wouldn't even compile that example because it gets to class smallcomponent and doesn't know what a bigthing is yet because it hasn't been declared.
    Easily dealt with using a forward declaration.




    Quote Originally Posted by lautarox
    Code:
    class SmallComponent {
    private:
        BigThing *outer;
    public:
        SmallComponent(BigThing *outer) : " outer(outer) {}" -> Here, why are you doing that?
    };
    The stuff after the semicolon and before the first open bracket is part of the initializer list. It is the preferred method of initialization of member variables. Instead of:
    Code:
    class bar
    {
        int a;
        double b;
        char c;
    public:
        bar()
        {
            a = 10;
            b = 45.5;
            c = 'c';
        }
    };
    The preferred method would look like:
    Code:
    class bar
    {
        int a;
        double b;
        char c;
    public:
        bar() :a(10), b(45.5), c('c') {}
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Is it possible to compile under g++?
    I've tryied compiling this with no success
    Code:
    #include <iostream> 
    using namespace std;
    
    class SmallComponent {
    private:
        BigThing *outer;
    public:
        void sumar() {
    	int x = outer->i+outer->j;
    	cout << x << endl;
        }
        SmallComponent(BigThing *outer) : outer(outer) {}
    };
    
    class BigThing {
    private:
        SmallComponent small;
    public:
        doSomething() {
    	small.sumar();
        }
        int i;
        int j;
        BigThing() : small(this), i(2), j(4) {}
    };
    
    
    int main() {
    	BigThing clase;
    	clase.doSomething();
    	return 0;
    }
    It returns:
    http://img684.imageshack.us/img684/2466/compile.jpg
    Sorry for the image link, I can't copy it

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Quote Originally Posted by lautarox
    Is it possible to compile under g++?
    You need to fix several problems, including the fact that you cannot actually use what the BigThing member pointer points to until after the BigThing class is defined, thus you cannot define those two member functions of SmallComponent in the class definition of SmallComponent.

    Just to get your example to compile, try:
    Code:
    #include <iostream> 
    
    class BigThing;
    
    class SmallComponent {
    public:
        SmallComponent(BigThing* outer);
        void sumar();
    private:
        BigThing* outer;
    };
    
    class BigThing {
    public:
        int i;
        int j;
    
        void doSomething() {
            small.sumar();
        }
    
        BigThing() : i(2), j(4), small(this) {}
    private:
        SmallComponent small;
    };
    
    SmallComponent::SmallComponent(BigThing* outer) : outer(outer) {}
    
    void SmallComponent::sumar() {
        using namespace std;
        cout << (outer->i + outer->j) << endl;
    }
    
    int main() {
        BigThing clase;
        clase.doSomething();
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Thanks! that was what I was looking for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Interview questions, please help
    By higaea in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2010, 07:35 AM
  2. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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