Thread: Mutually dependent classes

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Mutually dependent classes

    Let's say I have two mutually dependent classes, such as

    Code:
    <A.h>
    class B;
    class A { A(B b) };
    Code:
    <B.h>
    class B { foo(A a) };
    Code:
    <C.h>
    #include "A.h"
    #include "B.h"
    On GCC I get foward declaration/incomplete type errors. Suppose these two files are in two seperate header files. What is the right way to declare and define classes like this?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A forward declaration is only sufficient if you are using a pointer or reference to the type being forward-declared. Passing by value, in the example you provide, is not possible because the full definition of the type is not available yet.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To expand on what Brewbuck says: You litterally CAN NOT DO THAT.

    Use references, pointers or come up with another way of solving the entire problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    So the only way is to make the parameters const reference or reference (or pointer, I assume)?

    EDIT: Posted this before I saw matsp's post. Thanks, I guess I can use references as I feel the design is what I need.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    If you have circular dependency, you've either got bad code or you've got a special case where you can't avoid it.

    Exactly what kind of problem are you trying to solve? A little more information from you will help us to understand why you have a circular dependency and if there's a possible other solution in which you can avoid it.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I'm making a C++ regular expression wrapper class (wraps the C regex library) that is similiar to the Java.regex package.

    It's got two classes: Pattern and Matcher.

    The Pattern class has a function like:
    Matcher Pattern::match(std::string pattern)

    and the Matcher class needs to have a method such as:
    void Matcher::setPattern(Pattern)

    As you can see, I can't define the Pattern class without first defining the Matcher class, and vice-versa.
    Of course, I could change the argument into setPattern to a const reference and define the Matcher class before the Pattern class. I can't think of any other way to implement this design without circular dependency.
    Last edited by MacNilly; 03-16-2009 at 08:00 PM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacNilly View Post
    I'm making a C++ regular expression wrapper class (wraps the C regex library) that is similiar to the Java.regex package.

    It's got two classes: Pattern and Matcher.

    The Pattern class has a function like:
    Matcher Pattern::match(std::string pattern)

    and the Matcher class needs to have a method such as:
    void Matcher::setPattern(Pattern)
    In that case, setPattern() should take a const Pattern &, since it will presumably compile it down internally, and there's no real need to pass by value. On the other hand, returning a Matcher by value from Pattern::match() is the right way to go.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MacNilly View Post
    I'm making a C++ regular expression wrapper class (wraps the C regex library) that is similiar to the Java.regex package.
    Is the Boost Regex library insufficient for your needs?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Probably not, but I'm doing this as a personal project to learn the C regex library.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You only need a forward declaration of a type to write a declaration of a function that takes or returns that type by value. You only need the full definition when you define the function. In other words, this is perfectly fine:
    Code:
    struct A;
    
    struct B {
      A frabuzzle(A);
    };
    But you need the full definition of A before you can implement B::frabuzzle.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM