Help for correct class structure

Discussion in 'C++' started by me.devilspride@gmail.com, Apr 4, 2008.

  1. Guest

    Hi,

    I have two classes A and B as follows.
    A has member data X and member functions P, Q, R. All P,Q,R access X
    and Q calls P.
    B is a derived class of another Class(which I don't go into) with
    member functions D, E.

    P in A calls D in B.
    D in B calls Q, R in A.

    It's a big recursion. Everything works if I move D,E to class A. But
    how do I achieve above as value of X need to be used through the whole
    program execution? I wanted to know if what I am trying to accomplish
    is something doable or I need to redesign my class structures.

    Thanks
     
    , Apr 4, 2008
    #1
    1. Advertising

  2. Jim Langston Guest

    wrote:
    > Hi,
    >
    > I have two classes A and B as follows.
    > A has member data X and member functions P, Q, R. All P,Q,R access X
    > and Q calls P.
    > B is a derived class of another Class(which I don't go into) with
    > member functions D, E.
    >
    > P in A calls D in B.
    > D in B calls Q, R in A.
    >
    > It's a big recursion. Everything works if I move D,E to class A. But
    > how do I achieve above as value of X need to be used through the whole
    > program execution? I wanted to know if what I am trying to accomplish
    > is something doable or I need to redesign my class structures.
    >
    > Thanks


    How about showing some actual code? Even something like:

    class A
    {
    int X;
    public:
    P() {}
    Q() {}
    // ...
    };

    etc..

    What your'e trying to describe in words is not clear.


    --
    Jim Langston
     
    Jim Langston, Apr 5, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    > wrote:
    > > Hi,

    >
    > > I have two classes A and B as follows.
    > > A has member data X and member functions P, Q, R. All P,Q,R access X
    > > and Q calls P.
    > > B is a derived class of another Class(which I don't go into) with
    > > member functions D, E.

    >
    > > P in A calls D in B.
    > > D in B calls Q, R in A.

    >
    > > It's a big recursion. Everything works if I move D,E to class A. But
    > > how do I achieve above as value of X need to be used through the whole
    > > program execution? I wanted to know if what I am trying to accomplish
    > > is something doable or I need to redesign my class structures.

    >
    > > Thanks

    >
    > How about showing some actual code?  Even something like:
    >
    > class A
    > {
    >    int X;
    > public:
    >    P() {}
    >    Q() {}
    >    // ...
    >
    > };
    >
    > etc..
    >
    > What your'e trying to describe in words is not clear.
    >
    > --
    > Jim Langston
    >


    I am sorry for that

    class A {
    int X;
    public:
    P() { Q(); }
    Q() { D(); R(); }
    R() { uses X }
    };

    class B: public someclass {
    //inherited data from someclass
    public:
    D() {
    Q(); //end condition for recursion depends on X
    R();
    }
    };

    I can't create D() in A as D() access private data in B. But D() calls
    both Q() and R() and value of X has to be same everywhere. i.e. R()
    called from D access the same X value that was used by class A. I
    guess I need to pass around this pointer of A's object or X. I guess
    it shouldn't be difficult but I am just blocked.
    Hope this helps.
     
    , Apr 5, 2008
    #3
  4. Jim Langston Guest

    wrote:
    > On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    >> wrote:
    >>> Hi,

    >>
    >>> I have two classes A and B as follows.
    >>> A has member data X and member functions P, Q, R. All P,Q,R access X
    >>> and Q calls P.
    >>> B is a derived class of another Class(which I don't go into) with
    >>> member functions D, E.

    >>
    >>> P in A calls D in B.
    >>> D in B calls Q, R in A.

    >>
    >>> It's a big recursion. Everything works if I move D,E to class A. But
    >>> how do I achieve above as value of X need to be used through the
    >>> whole program execution? I wanted to know if what I am trying to
    >>> accomplish is something doable or I need to redesign my class
    >>> structures.

    >>
    >>> Thanks

    >>
    >> How about showing some actual code? Even something like:
    >>
    >> class A
    >> {
    >> int X;
    >> public:
    >> P() {}
    >> Q() {}
    >> // ...
    >>
    >> };
    >>
    >> etc..
    >>
    >> What your'e trying to describe in words is not clear.
    >>
    >> --
    >> Jim Langston
    >>

    >
    > I am sorry for that
    >
    > class A {
    > int X;
    > public:
    > P() { Q(); }
    > Q() { D(); R(); }
    > R() { uses X }
    > };
    >
    > class B: public someclass {
    > //inherited data from someclass
    > public:
    > D() {
    > Q(); //end condition for recursion depends on X
    > R();
    > }
    > };
    >
    > I can't create D() in A as D() access private data in B. But D() calls
    > both Q() and R() and value of X has to be same everywhere. i.e. R()
    > called from D access the same X value that was used by class A. I
    > guess I need to pass around this pointer of A's object or X. I guess
    > it shouldn't be difficult but I am just blocked.
    > Hope this helps.


    There is no relationship between A and B in your code. A does not create an
    instance of B, or B A, A does not derive from B, nor B A. How does Q know
    which instance of B to call D for? How does D know which instance of A to
    call Q for? You either have to pass a B to A and/or an A to B.
    class A {
    {
    int X;
    public:
    P(B& b) { Q( b ); }
    Q(B& b) { b.D( this ); )
    R( ) { uses X )
    };

    class B: public someclass {
    public:
    D( A& a ) {
    a.Q( this );
    a.R();
    }


    --
    Jim Langston
     
    Jim Langston, Apr 5, 2008
    #4
  5. Guest

    On Apr 4, 8:52 pm, "Jim Langston" <> wrote:
    > wrote:
    > > On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    > >> wrote:
    > >>> Hi,

    >
    > >>> I have two classes A and B as follows.
    > >>> A has member data X and member functions P, Q, R. All P,Q,R access X
    > >>> and Q calls P.
    > >>> B is a derived class of another Class(which I don't go into) with
    > >>> member functions D, E.

    >
    > >>> P in A calls D in B.
    > >>> D in B calls Q, R in A.

    >
    > >>> It's a big recursion. Everything works if I move D,E to class A. But
    > >>> how do I achieve above as value of X need to be used through the
    > >>> whole program execution? I wanted to know if what I am trying to
    > >>> accomplish is something doable or I need to redesign my class
    > >>> structures.

    >
    > >>> Thanks

    >
    > >> How about showing some actual code? Even something like:

    >
    > >> class A
    > >> {
    > >> int X;
    > >> public:
    > >> P() {}
    > >> Q() {}
    > >> // ...

    >
    > >> };

    >
    > >> etc..

    >
    > >> What your'e trying to describe in words is not clear.

    >
    > >> --
    > >> Jim Langston
    > >>

    >
    > > I am sorry for that

    >
    > > class A {
    > >    int X;
    > > public:
    > >    P() { Q(); }
    > >    Q() { D(); R(); }
    > >    R() { uses X }
    > > };

    >
    > > class B: public someclass {
    > >    //inherited data from someclass
    > > public:
    > >    D() {
    > >        Q(); //end condition for recursion depends on X
    > >        R();
    > >    }
    > > };

    >
    > > I can't create D() in A as D() access private data in B. But D() calls
    > > both Q() and R() and value of X has to be same everywhere. i.e. R()
    > > called from D access the same X value that was used by class A. I
    > > guess I need to pass around this pointer of A's object or X. I guess
    > > it shouldn't be difficult but I am just blocked.
    > > Hope this helps.

    >
    > There is no relationship between A and B in your code.  A does not create an
    > instance of B, or B A, A does not derive from B, nor B A.  How does Q know
    > which instance of B to call D for?  How does D know which instance of A to
    > call Q for?  You either have to pass a B to A and/or an A to B.
    > class A {
    > {
    >    int X;
    > public:
    >    P(B& b) { Q( b ); }
    >    Q(B& b) { b.D( this ); )
    >    R( ) { uses X )
    >
    > };
    >
    > class B: public someclass {
    > public:
    >    D( A& a ) {
    >       a.Q( this );
    >       a.R();
    >
    > }
    >
    > --
    > Jim Langston
    >


    Thanks for the reply, that actually was my problem. Actually in my
    case there will be just one instance of A and B. But there isn't just
    one class B, there are lots of classes derived from a parent class. I
    am actually creating objects for these classes, kind of linked list. A
    has functions for creating a list, but it may be nested that's why
    there is a recursion. I guess passing A to B in my case is good. Thank
    you.
     
    , Apr 5, 2008
    #5
  6. Guest

    On Apr 4, 9:34 pm, wrote:
    > On Apr 4, 8:52 pm, "Jim Langston" <> wrote:
    >
    >
    >
    > > wrote:
    > > > On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    > > >> wrote:
    > > >>> Hi,

    >
    > > >>> I have two classes A and B as follows.
    > > >>> A has member data X and member functions P, Q, R. All P,Q,R access X
    > > >>> and Q calls P.
    > > >>> B is a derived class of another Class(which I don't go into) with
    > > >>> member functions D, E.

    >
    > > >>> P in A calls D in B.
    > > >>> D in B calls Q, R in A.

    >
    > > >>> It's a big recursion. Everything works if I move D,E to class A. But
    > > >>> how do I achieve above as value of X need to be used through the
    > > >>> whole program execution? I wanted to know if what I am trying to
    > > >>> accomplish is something doable or I need to redesign my class
    > > >>> structures.

    >
    > > >>> Thanks

    >
    > > >> How about showing some actual code? Even something like:

    >
    > > >> class A
    > > >> {
    > > >> int X;
    > > >> public:
    > > >> P() {}
    > > >> Q() {}
    > > >> // ...

    >
    > > >> };

    >
    > > >> etc..

    >
    > > >> What your'e trying to describe in words is not clear.

    >
    > > >> --
    > > >> Jim Langston
    > > >>

    >
    > > > I am sorry for that

    >
    > > > class A {
    > > >    int X;
    > > > public:
    > > >    P() { Q(); }
    > > >    Q() { D(); R(); }
    > > >    R() { uses X }
    > > > };

    >
    > > > class B: public someclass {
    > > >    //inherited data from someclass
    > > > public:
    > > >    D() {
    > > >        Q(); //end condition for recursion depends on X
    > > >        R();
    > > >    }
    > > > };

    >
    > > > I can't create D() in A as D() access private data in B. But D() calls
    > > > both Q() and R() and value of X has to be same everywhere. i.e. R()
    > > > called from D access the same X value that was used by class A. I
    > > > guess I need to pass around this pointer of A's object or X. I guess
    > > > it shouldn't be difficult but I am just blocked.
    > > > Hope this helps.

    >
    > > There is no relationship between A and B in your code.  A does not create an
    > > instance of B, or B A, A does not derive from B, nor B A.  How does Q know
    > > which instance of B to call D for?  How does D know which instance of A to
    > > call Q for?  You either have to pass a B to A and/or an A to B.
    > > class A {
    > > {
    > >    int X;
    > > public:
    > >    P(B& b) { Q( b ); }
    > >    Q(B& b) { b.D( this ); )
    > >    R( ) { uses X )

    >
    > > };

    >
    > > class B: public someclass {
    > > public:
    > >    D( A& a ) {
    > >       a.Q( this );
    > >       a.R();

    >
    > > }

    >
    > > --
    > > Jim Langston
    > >

    >
    > Thanks for the reply, that actually was my problem. Actually in my
    > case there will be just one instance of A and B. But there isn't just
    > one class B, there are lots of classes derived from a parent class. I
    > am actually creating objects for these classes, kind of linked list. A
    > has functions for creating a list, but it may be nested that's why
    > there is a recursion. I guess passing A to B in my case is good. Thank
    > you.


    I still have some problems, Here A creates an instance of B and calls
    member function of B.

    ///A.h
    class A {
    {
    int X;
    public:
    P() { Q(); }
    Q() {
    R();
    bb = new B();
    bb.D( this ); )
    R() { uses X )

    };

    ///B.h
    class B: public someclass {
    public:
    D( A& a ) {
    a.Q();
    a.R();
    };

    But there is a cyclic dependency and forward declaration of A in B
    doesn't work. What should I do?
    Any help would be very appreciated. Thanks.
     
    , Apr 5, 2008
    #6
  7. On 5 Apr, 21:08, wrote:
    > On Apr 4, 9:34 pm, wrote:
    >
    >
    >
    >
    >
    > > On Apr 4, 8:52 pm, "Jim Langston" <> wrote:

    >
    > > > wrote:
    > > > > On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    > > > >> wrote:
    > > > >>> Hi,

    >
    > > > >>> I have two classes A and B as follows.
    > > > >>> A has member data X and member functions P, Q, R. All P,Q,R access X
    > > > >>> and Q calls P.
    > > > >>> B is a derived class of another Class(which I don't go into) with
    > > > >>> member functions D, E.

    >
    > > > >>> P in A calls D in B.
    > > > >>> D in B calls Q, R in A.

    >
    > > > >>> It's a big recursion. Everything works if I move D,E to class A. But
    > > > >>> how do I achieve above as value of X need to be used through the
    > > > >>> whole program execution? I wanted to know if what I am trying to
    > > > >>> accomplish is something doable or I need to redesign my class
    > > > >>> structures.

    >
    > > > >>> Thanks

    >
    > > > >> How about showing some actual code? Even something like:

    >
    > > > >> class A
    > > > >> {
    > > > >> int X;
    > > > >> public:
    > > > >> P() {}
    > > > >> Q() {}
    > > > >> // ...

    >
    > > > >> };

    >
    > > > >> etc..

    >
    > > > >> What your'e trying to describe in words is not clear.

    >
    > > > >> --
    > > > >> Jim Langston
    > > > >>

    >
    > > > > I am sorry for that

    >
    > > > > class A {
    > > > >    int X;
    > > > > public:
    > > > >    P() { Q(); }
    > > > >    Q() { D(); R(); }
    > > > >    R() { uses X }
    > > > > };

    >
    > > > > class B: public someclass {
    > > > >    //inherited data from someclass
    > > > > public:
    > > > >    D() {
    > > > >        Q(); //end condition for recursion depends on X
    > > > >        R();
    > > > >    }
    > > > > };

    >
    > > > > I can't create D() in A as D() access private data in B. But D() calls
    > > > > both Q() and R() and value of X has to be same everywhere. i.e. R()
    > > > > called from D access the same X value that was used by class A. I
    > > > > guess I need to pass around this pointer of A's object or X. I guess
    > > > > it shouldn't be difficult but I am just blocked.
    > > > > Hope this helps.

    >
    > > > There is no relationship between A and B in your code.  A does not create an
    > > > instance of B, or B A, A does not derive from B, nor B A.  How does Q know
    > > > which instance of B to call D for?  How does D know which instance of A to
    > > > call Q for?  You either have to pass a B to A and/or an A to B.
    > > > class A {
    > > > {
    > > >    int X;
    > > > public:
    > > >    P(B& b) { Q( b ); }
    > > >    Q(B& b) { b.D( this ); )
    > > >    R( ) { uses X )

    >
    > > > };

    >
    > > > class B: public someclass {
    > > > public:
    > > >    D( A& a ) {
    > > >       a.Q( this );
    > > >       a.R();

    >
    > > > }

    >
    > > > --
    > > > Jim Langston
    > > >

    >
    > > Thanks for the reply, that actually was my problem. Actually in my
    > > case there will be just one instance of A and B. But there isn't just
    > > one class B, there are lots of classes derived from a parent class. I
    > > am actually creating objects for these classes, kind of linked list. A
    > > has functions for creating a list, but it may be nested that's why
    > > there is a recursion. I guess passing A to B in my case is good. Thank
    > > you.

    >
    > I still have some problems, Here A creates an instance of B and calls
    > member function of B.
    >
    > ///A.h
    > class A {
    > {
    >    int X;
    > public:
    >    P() { Q(); }
    >    Q() {
    >       R();
    >       bb = new B();
    >       bb.D( this ); )
    >    R() { uses X )
    >
    > };
    >
    > ///B.h
    > class B: public someclass {
    > public:
    >    D( A& a ) {
    >       a.Q();
    >       a.R();
    >
    > };
    >
    > But there is a cyclic dependency and forward declaration of A in B
    > doesn't work. What should I do?
    > Any help would be very appreciated. Thanks.- Hide quoted text -
    >
    > - Show quoted text -


    You're thinking in Java/C# terms - separate the interface from
    the implementation and all will be well:

    // A.h
    class A {
    int X;
    public:
    void Q();
    void R();
    };

    ///B.h
    class B: public someclass {
    public:
    D( A& a );
    };

    //A.cpp
    #include "A.h"
    #include "B.h"
    void A::Q() {
    R();
    bb = new B();
    bb.D( this );
    }
    void A::R() { uses X }

    //B.cpp
    #include "B.h"
    #include "A.h"
    void B::D( A& a ) {
    a.Q();
    a.R();
    }
     
    tragomaskhalos, Apr 5, 2008
    #7
  8. Guest

    On Apr 5, 5:20 pm, tragomaskhalos <>
    wrote:
    > On 5 Apr, 21:08, wrote:
    >
    >
    >
    > > On Apr 4, 9:34 pm, wrote:

    >
    > > > On Apr 4, 8:52 pm, "Jim Langston" <> wrote:

    >
    > > > > wrote:
    > > > > > On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    > > > > >> wrote:
    > > > > >>> Hi,

    >
    > > > > >>> I have two classes A and B as follows.
    > > > > >>> A has member data X and member functions P, Q, R. All P,Q,R access X
    > > > > >>> and Q calls P.
    > > > > >>> B is a derived class of another Class(which I don't go into) with
    > > > > >>> member functions D, E.

    >
    > > > > >>> P in A calls D in B.
    > > > > >>> D in B calls Q, R in A.

    >
    > > > > >>> It's a big recursion. Everything works if I move D,E to class A. But
    > > > > >>> how do I achieve above as value of X need to be used through the
    > > > > >>> whole program execution? I wanted to know if what I am trying to
    > > > > >>> accomplish is something doable or I need to redesign my class
    > > > > >>> structures.

    >
    > > > > >>> Thanks

    >
    > > > > >> How about showing some actual code? Even something like:

    >
    > > > > >> class A
    > > > > >> {
    > > > > >> int X;
    > > > > >> public:
    > > > > >> P() {}
    > > > > >> Q() {}
    > > > > >> // ...

    >
    > > > > >> };

    >
    > > > > >> etc..

    >
    > > > > >> What your'e trying to describe in words is not clear.

    >
    > > > > >> --
    > > > > >> Jim Langston
    > > > > >>

    >
    > > > > > I am sorry for that

    >
    > > > > > class A {
    > > > > >    int X;
    > > > > > public:
    > > > > >    P() { Q(); }
    > > > > >    Q() { D(); R(); }
    > > > > >    R() { uses X }
    > > > > > };

    >
    > > > > > class B: public someclass {
    > > > > >    //inherited data from someclass
    > > > > > public:
    > > > > >    D() {
    > > > > >        Q(); //end condition for recursion depends on X
    > > > > >        R();
    > > > > >    }
    > > > > > };

    >
    > > > > > I can't create D() in A as D() access private data in B. But D() calls
    > > > > > both Q() and R() and value of X has to be same everywhere. i.e. R()
    > > > > > called from D access the same X value that was used by class A. I
    > > > > > guess I need to pass around this pointer of A's object or X. I guess
    > > > > > it shouldn't be difficult but I am just blocked.
    > > > > > Hope this helps.

    >
    > > > > There is no relationship between A and B in your code.  A does not create an
    > > > > instance of B, or B A, A does not derive from B, nor B A.  How does Q know
    > > > > which instance of B to call D for?  How does D know which instance of A to
    > > > > call Q for?  You either have to pass a B to A and/or an A to B.
    > > > > class A {
    > > > > {
    > > > >    int X;
    > > > > public:
    > > > >    P(B& b) { Q( b ); }
    > > > >    Q(B& b) { b.D( this ); )
    > > > >    R( ) { uses X )

    >
    > > > > };

    >
    > > > > class B: public someclass {
    > > > > public:
    > > > >    D( A& a ) {
    > > > >       a.Q( this );
    > > > >       a.R();

    >
    > > > > }

    >
    > > > > --
    > > > > Jim Langston
    > > > >

    >
    > > > Thanks for the reply, that actually was my problem. Actually in my
    > > > case there will be just one instance of A and B. But there isn't just
    > > > one class B, there are lots of classes derived from a parent class. I
    > > > am actually creating objects for these classes, kind of linked list. A
    > > > has functions for creating a list, but it may be nested that's why
    > > > there is a recursion. I guess passing A to B in my case is good. Thank
    > > > you.

    >
    > > I still have some problems, Here A creates an instance of B and calls
    > > member function of B.

    >
    > > ///A.h
    > > class A {
    > > {
    > >    int X;
    > > public:
    > >    P() { Q(); }
    > >    Q() {
    > >       R();
    > >       bb = new B();
    > >       bb.D( this ); )
    > >    R() { uses X )

    >
    > > };

    >
    > > ///B.h
    > > class B: public someclass {
    > > public:
    > >    D( A& a ) {
    > >       a.Q();
    > >       a.R();

    >
    > > };

    >
    > > But there is a cyclic dependency and forward declaration of A in B
    > > doesn't work. What should I do?
    > > Any help would be very appreciated. Thanks.- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > You're thinking in Java/C# terms - separate the interface from
    > the implementation and all will be well:
    >
    > // A.h
    > class A {
    >     int X;
    >  public:
    >     void Q();
    >     void R();
    >
    > };
    >
    > ///B.h
    > class B: public someclass {
    > public:
    >    D( A& a );
    >
    > };
    >
    > //A.cpp
    > #include "A.h"
    > #include "B.h"
    > void A::Q() {
    >        R();
    >        bb = new B();
    >        bb.D( this );}
    >
    > void A::R() { uses X }
    >
    > //B.cpp
    > #include "B.h"
    > #include "A.h"
    > void B::D( A& a ) {
    >       a.Q();
    >       a.R();
    >
    > }
    >
    >


    OK, but adding header files that way wouldn't cause any problems. I
    did the same but got errors like - "C++ forbids declaration with no
    type". But I will again try, Thanks.
     
    , Apr 6, 2008
    #8
  9. On 6 Apr, 00:07, wrote:
    > On Apr 5, 5:20 pm, tragomaskhalos <>
    > wrote:
    >
    >
    >
    >
    >
    > > On 5 Apr, 21:08, wrote:

    >
    > > > On Apr 4, 9:34 pm, wrote:

    >
    > > > > On Apr 4, 8:52 pm, "Jim Langston" <> wrote:

    >
    > > > > > wrote:
    > > > > > > On Apr 4, 7:28 pm, "Jim Langston" <> wrote:
    > > > > > >> wrote:
    > > > > > >>> Hi,

    >
    > > > > > >>> I have two classes A and B as follows.
    > > > > > >>> A has member data X and member functions P, Q, R. All P,Q,R access X
    > > > > > >>> and Q calls P.
    > > > > > >>> B is a derived class of another Class(which I don't go into) with
    > > > > > >>> member functions D, E.

    >
    > > > > > >>> P in A calls D in B.
    > > > > > >>> D in B calls Q, R in A.

    >
    > > > > > >>> It's a big recursion. Everything works if I move D,E to class A. But
    > > > > > >>> how do I achieve above as value of X need to be used through the
    > > > > > >>> whole program execution? I wanted to know if what I am trying to
    > > > > > >>> accomplish is something doable or I need to redesign my class
    > > > > > >>> structures.

    >
    > > > > > >>> Thanks

    >
    > > > > > >> How about showing some actual code? Even something like:

    >
    > > > > > >> class A
    > > > > > >> {
    > > > > > >> int X;
    > > > > > >> public:
    > > > > > >> P() {}
    > > > > > >> Q() {}
    > > > > > >> // ...

    >
    > > > > > >> };

    >
    > > > > > >> etc..

    >
    > > > > > >> What your'e trying to describe in words is not clear.

    >
    > > > > > >> --
    > > > > > >> Jim Langston
    > > > > > >>

    >
    > > > > > > I am sorry for that

    >
    > > > > > > class A {
    > > > > > >    int X;
    > > > > > > public:
    > > > > > >    P() { Q(); }
    > > > > > >    Q() { D(); R(); }
    > > > > > >    R() { uses X }
    > > > > > > };

    >
    > > > > > > class B: public someclass {
    > > > > > >    //inherited data from someclass
    > > > > > > public:
    > > > > > >    D() {
    > > > > > >        Q(); //end condition for recursion depends on X
    > > > > > >        R();
    > > > > > >    }
    > > > > > > };

    >
    > > > > > > I can't create D() in A as D() access private data in B. But D() calls
    > > > > > > both Q() and R() and value of X has to be same everywhere. i.e. R()
    > > > > > > called from D access the same X value that was used by class A. I
    > > > > > > guess I need to pass around this pointer of A's object or X. I guess
    > > > > > > it shouldn't be difficult but I am just blocked.
    > > > > > > Hope this helps.

    >
    > > > > > There is no relationship between A and B in your code.  A does not create an
    > > > > > instance of B, or B A, A does not derive from B, nor B A.  How does Q know
    > > > > > which instance of B to call D for?  How does D know which instance of A to
    > > > > > call Q for?  You either have to pass a B to A and/or an A to B.
    > > > > > class A {
    > > > > > {
    > > > > >    int X;
    > > > > > public:
    > > > > >    P(B& b) { Q( b ); }
    > > > > >    Q(B& b) { b.D( this ); )
    > > > > >    R( ) { uses X )

    >
    > > > > > };

    >
    > > > > > class B: public someclass {
    > > > > > public:
    > > > > >    D( A& a ) {
    > > > > >       a.Q( this );
    > > > > >       a.R();

    >
    > > > > > }

    >
    > > > > > --
    > > > > > Jim Langston
    > > > > >

    >
    > > > > Thanks for the reply, that actually was my problem. Actually in my
    > > > > case there will be just one instance of A and B. But there isn't just
    > > > > one class B, there are lots of classes derived from a parent class. I
    > > > > am actually creating objects for these classes, kind of linked list. A
    > > > > has functions for creating a list, but it may be nested that's why
    > > > > there is a recursion. I guess passing A to B in my case is good. Thank
    > > > > you.

    >
    > > > I still have some problems, Here A creates an instance of B and calls
    > > > member function of B.

    >
    > > > ///A.h
    > > > class A {
    > > > {
    > > >    int X;
    > > > public:
    > > >    P() { Q(); }
    > > >    Q() {
    > > >       R();
    > > >       bb = new B();
    > > >       bb.D( this ); )
    > > >    R() { uses X )

    >
    > > > };

    >
    > > > ///B.h
    > > > class B: public someclass {
    > > > public:
    > > >    D( A& a ) {
    > > >       a.Q();
    > > >       a.R();

    >
    > > > };

    >
    > > > But there is a cyclic dependency and forward declaration of A in B
    > > > doesn't work. What should I do?
    > > > Any help would be very appreciated. Thanks.- Hide quoted text -

    >
    > > > - Show quoted text -

    >
    > > You're thinking in Java/C# terms - separate the interface from
    > > the implementation and all will be well:

    >
    > > // A.h
    > > class A {
    > >     int X;
    > >  public:
    > >     void Q();
    > >     void R();

    >
    > > };

    >
    > > ///B.h
    > > class B: public someclass {
    > > public:
    > >    D( A& a );

    >
    > > };

    >
    > > //A.cpp
    > > #include "A.h"
    > > #include "B.h"
    > > void A::Q() {
    > >        R();
    > >        bb = new B();
    > >        bb.D( this );}

    >
    > > void A::R() { uses X }

    >
    > > //B.cpp
    > > #include "B.h"
    > > #include "A.h"
    > > void B::D( A& a ) {
    > >       a.Q();
    > >       a.R();

    >
    > > }

    >
    > OK, but adding header files that way wouldn't cause any problems. I
    > did the same but got errors like - "C++ forbids declaration with no
    > type". But I will again try, Thanks.- Hide quoted text -
    >
    > - Show quoted text -


    Sorry, I forgot the required forward declarations
    in the B header:

    ///B.h
    class A; // <===== forgot this.
    class B: public someclass {
    public:
    D( A& a );
    };
     
    tragomaskhalos, Apr 6, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    528
    Roedy Green
    Jul 8, 2003
  2. E11
    Replies:
    1
    Views:
    4,903
    Thomas Weidenfeller
    Oct 12, 2005
  3. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    461
  4. J.Ram
    Replies:
    7
    Views:
    670
  5. froil
    Replies:
    12
    Views:
    321
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page