How get two classes to know about each other?

This is a discussion on How get two classes to know about each other? within the C++ Programming forums, part of the General Programming Boards category; Sorry if this is obvious (it's a bit diff. from java). I have two classes declared in the same file ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How get two classes to know about each other?

    Sorry if this is obvious (it's a bit diff. from java). I have two classes declared in the same file and both refer to each other in their code. So the one that's declared first won't compile. How do I fix this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to forward declare one of the classes, e.g.:
    Code:
    class B;
    
    class A
    {
    ... 
       B *b;
    };
    
    class B
    {
       ...
    };
    --
    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.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You need to make a forward declaration of the second class. That way the first will know about it. A bit like a function prototype.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by 6tr6tr View Post
    Sorry if this is obvious (it's a bit diff. from java). I have two classes declared in the same file and both refer to each other in their code. So the one that's declared first won't compile. How do I fix this?
    Code:
    class second_class;
    
    class first_class
    {
        ...
    };
    
    class second_class
    {
        ...
    };
    Note that again, this isn't Java. With a forward declaration, you are only allowed to refer to the second_class through a pointer or reference. And if you have inline functions inside first_class, they will not be able to access the members of second_class since it has not been defined yet.

    EDIT: Hah! We all posted at the same exact MINUTE.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Thanks. Does this change if one class needs to call the constructor of the other and the other class needs to call a method from the first class? Is this possible?

    Example:

    Code:
    class Loader
    {
    public:
         static void load(std::string name)
         {
              //Do some loading here
              ....elided...
    
              if ( problemOccurred ) throw( new MYException( name ) );
         }
    };
    
    class MyException : std::exception
    {
    public:
         MyException(std::string name) { ...elided... }
    
         virtual const char* std::exception::what() 	const throw ()
         {
              //Do some loading here
              Loader.load( name );
              ....elided....
         }
    };

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by 6tr6tr View Post
    Thanks. Does this change if one class needs to call the constructor of the other and the other class needs to call a method from the first class? Is this possible?
    This PARTICULAR example is okay (I think), but in general, using inline functions in classes which refer to each other isn't going to work. Try to get into the habit of writing your functions out-of-line, instead of in the class definition.

    EDIT: Also, having an exception calling the Loader is truly weird. I see a great potential for an infinite loop of exception throwing, here. You declare what() as throw(), and yet that is a lie, because it calls Loader::load, which can throw.

    I doubt you've actually tried compiling that. There are still Java-isms floating around. It's Loader::load, not Loader.load.
    Last edited by brewbuck; 04-11-2008 at 09:56 AM.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    This PARTICULAR example is okay (I think), but in general, using inline functions in classes which refer to each other isn't going to work. Try to get into the habit of writing your functions out-of-line, instead of in the class definition.

    EDIT: Also, having an exception calling the Loader is truly weird. I see a great potential for an infinite loop of exception throwing, here.
    Don't worry, there's no chance of looping (the other constructors/methods work completely differently).

    Question: I was originally doing the first class as just functions outside a class but that wasn't working since it needed knowledge of the constructor of the exception class (which needed knowledge of the functions). So how would I do that?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It should work to write the functions outside of the class, as long as you have declared the existance of both classes.

    --
    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.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by 6tr6tr View Post
    I was originally doing the first class as just functions outside a class but that wasn't working since it needed knowledge of the constructor of the exception class (which needed knowledge of the functions). So how would I do that?
    Honestly I kind of don't want to help you take this perversion any farther than it already is I don't agree that it can't infinitely loop, I think it clearly can. And the throw() clause is still a lie. And actually doing "stuff" inside the what() method of an exception is not a good idea.

    But if you can cough up an example of how you were trying to do it before (a real piece of code, not a paraphrase), I'll see what I can tell you.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    OK, I figured it out. I do a forward declaration of the function and then put the actual definition after the class. Nice!

    Still, it is weird to program where classes have so much trouble knowing about each other like that.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by 6tr6tr View Post
    OK, I figured it out. I do a forward declaration of the function and then put the actual definition after the class. Nice!

    Still, it is weird to program where classes have so much trouble knowing about each other like that.
    The trouble is just syntactic. If you write your functions out-of-line you will not encounter these problems.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    Honestly I kind of don't want to help you take this perversion any farther than it already is I don't agree that it can't infinitely loop, I think it clearly can. And the throw() clause is still a lie. And actually doing "stuff" inside the what() method of an exception is not a good idea.

    But if you can cough up an example of how you were trying to do it before (a real piece of code, not a paraphrase), I'll see what I can tell you.
    Don't worry I'm not really doing anything inside the what(). I just wanted to present a clear example that concentrated only on the problem I was having. The actual class' code is very different from what i presented here but irrelevant.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    The trouble is just syntactic. If you write your functions out-of-line you will not encounter these problems.
    I'm sorry, I don't know what an out-of-line function is versus an inline one. Can you give me an example? Thanks!

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If what() lets an exception escape, the program either aborts because an exception was thrown during unwind, or it eventually runs out of try blocks and aborts anyway. So no, it can't loop indefinitely

    The above should not encourage you to use such a design.


    In line:
    Code:
    class foo
    {
      void bar()
      {
        // code here
      }
    };
    Out of line:
    Code:
    class foo
    {
      void bar();
    };
    
    void foo::bar()
    {
      // code here
    }
    There are further rules concerning where you place what part of the code - generally, the class definition goes into a header (.h or .hpp) and the out-of-line function definitions go into a source file (.cpp).
    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

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by 6tr6tr View Post
    I'm sorry, I don't know what an out-of-line function is versus an inline one. Can you give me an example? Thanks!
    This is inline:

    Code:
    class A
    {
        void foo()
        {
            do_something();
        }
    };
    This is out of line:

    Code:
    class A
    {
        void foo();
    };
    
    // Then in a .cpp file somewhere:
    
    void A::foo()
    {
        do_something();
    }

Page 1 of 2 12 LastLast
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. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 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

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