Results 1 to 2 of 2

Thread: How to realize it?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default How to realize it?

    I have created class A in a.h and a.cpp,and class B in b.h and b.cpp;

    One method of A needs B,like this,
    Qt Code:
    1. class A
    2. {
    3. ...
    4. void method_A1(B b);
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    So the file b.h needs to be included in a.h.

    now one method of B needs A,like this,

    Qt Code:
    1. class B
    2. {
    3. ...
    4. void method_B1(A b);
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    So the file a.h needs to be included in b.h.

    the program runs normally,but I think the two header files are included each other,it is odd.

    Could you tell me how to deal with it normally?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to realize it?

    As you have explained the situation, where the parameters are passed by value (i.e. copied), you have done the normal thing. In order to write code that uses A the compiler needs to know the precise size of B, i.e. the declaration. The headers a.h and b.h should have include guards to avoid an endless loop.

    If method_x1() takes a reference or pointer to an A or B, rather than the copy you showed, then you can use forward declarations to break the header file dependency:
    Qt Code:
    1. // a.h
    2. #ifndef A_H
    3. #define A_H
    4.  
    5. class B; // forward declaration only
    6. class A
    7. {
    8. public:
    9. void method_A1(const B &b);
    10. void method_A2(B *b);
    11.  
    12. };
    13. #endif
    14.  
    15.  
    16. // a.cpp
    17. #include "a.h"
    18. #include "b.h"
    19.  
    20. void A::method_A1(const B &b) {}
    21. void A::method_A2(B *b) {}
    22.  
    23.  
    24. // b.h
    25. #ifndef B_H
    26. #define B_H
    27.  
    28. class A; // forward declaration only
    29. class B
    30. {
    31. public:
    32. void method_B1(A &a);
    33. void method_B2(const A *a);
    34. };
    35. #endif
    36.  
    37. // b.cpp
    38. #include "b.h"
    39. #include "a.h"
    40.  
    41. void B::method_B1(A &a) {}
    42. void B::method_B2(const A *a) {}
    To copy to clipboard, switch view to plain text mode 
    The compiler then requires only a pointer to B, of known size, to build code using A. It no longer requires the full declaration of B. The full declaration of B will be required in the implementation of A.

    In large libraries with lots of inter-relationships these measures can reduce compilation time quite a bit by reducing the amount of pre-processor material to be handled.

Similar Threads

  1. How to realize the hotKey function?
    By cspp in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2009, 11:12
  2. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 07:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.