Re: circular dependencies & globals - avoid w/ precompiler directives?betterdesign suggestions?

Discussion in 'C++' started by Suzanne Vogel, Jun 26, 2003.

  1. I posted my failed attempt to solve the problem -- without splitting up
    a file -- here:
    http://www.cs.unc.edu/~vogel/playpen/c /std/CircularDependencies/
    It gives a link error (which I commented), indicating that I replaced
    the problem of circular includes with too *few* includes. Blegh.

    I don't blame you if you don't want to look at it, and you should *not*
    need to look at it to answer my question. Maybe there's a standard
    solution to the circular dependency problem that I'm unaware of.

    I know that if I make your answering depend on reading too much, I won't
    get any answers to my dependency questions. ;)

    Suzanne
    Suzanne Vogel, Jun 26, 2003
    #1
    1. Advertising

  2. Re: circular dependencies & globals - avoid w/ precompiler directives?better design suggestions?

    "Suzanne Vogel" <> wrote...
    > I posted my failed attempt to solve the problem -- without splitting up
    > a file -- here:
    > http://www.cs.unc.edu/~vogel/playpen/c /std/CircularDependencies/
    > It gives a link error (which I commented), indicating that I replaced
    > the problem of circular includes with too *few* includes. Blegh.
    >
    > I don't blame you if you don't want to look at it, and you should *not*
    > need to look at it to answer my question. Maybe there's a standard
    > solution to the circular dependency problem that I'm unaware of.
    >
    > I know that if I make your answering depend on reading too much, I won't
    > get any answers to my dependency questions. ;)


    Here is my solution with a bit of explanation.
    ----------------------------------------- TestClass.h
    #ifndef TESTCLASS_H // multiple inclusion guards -- GOOD!
    #define TESTCLASS_H
    #pragma message("### TestClass.h ###")

    // Define the class -- nothing else is needed here.
    class TestClass {
    private:
    int t;
    public:
    TestClass(int t=33);
    void print();
    };

    #endif // TESTCLASS_H
    ---------------------------------------- TestGlobals.h
    // File: TestGlobals.h
    // Note: Header guards *within* file replace those
    // that normally go around *entire* file

    // TestGlobals depends on TestClass (for "TestClass obj"), and
    // VB: ^^^^^^^^^^^^^^^^^^^^^^^^ No, it does NOT
    // TestClass depends on TestGlobals (for "int x")
    // VB: ^^^^^^^^^^^^^^^^^^^^^^^^ No, it does NOT

    #ifndef TESTGLOBALS_H
    #define TESTGLOBALS_H
    #pragma message("### TestGlobals.h >> extern int x ###")
    extern int x;

    extern class TestClass obj;

    #endif
    ----------------------------------------- TestClass.cpp
    // File: TestClass.cpp
    #pragma message("### TestClass.cpp ###")

    #include <TestClass.h>

    TestClass::TestClass(int t) : t(t)
    {
    }

    #include <iostream>

    void TestClass::print()
    {
    std::cout << "t: " << this->t << "\n";
    }
    -------------------------------------------- TestGlobals.cpp
    // File: TestGlobals.cpp
    #pragma message("### TestGlobals.cpp ###")
    #include "TestGlobals.h"

    int x = 99;

    #include <Testclass.h>
    TestClass obj(33);
    -------------------------------------------- TestMain.cpp
    // File: TestMain.cpp
    #pragma message("### TestMain.cpp ###")

    #include <TestClass.h>
    #include <TestGlobals.h>

    int main(int argc, char** argv)
    {
    obj.print();

    TestClass testObj(66);
    testObj.print();

    return 0;
    }
    -----------------------------------------------------------

    If you have more questions, ask.

    Victor
    Victor Bazarov, Jun 26, 2003
    #2
    1. Advertising

  3. Victor,

    Thanks! I tried it, and it works. I see why: In a header file, you put
    "extern class [something]" (instead of just "extern [something]"), and
    in the corresponding non-header file, you actually put the #include to
    the header for that class.

    Yes, sometimes my translations from C++ to English are a bit shaky. Next
    time I'll include source code from the start.

    I'm new to this newsgroup: I just joined ~a week ago. I'm amazed how
    helpful people are. I'll try to help when I can.

    Suzanne

    Victor Bazarov wrote:
    > Here is my solution with a bit of explanation.

    ....
    Suzanne Vogel, Jun 26, 2003
    #3
    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. crichmon
    Replies:
    3
    Views:
    746
    crichmon
    Jun 28, 2004
  2. Dylan
    Replies:
    7
    Views:
    544
    Dylan
    Jul 7, 2004
  3. Kiuhnm
    Replies:
    16
    Views:
    721
    Jonathan Mcdougall
    Jan 3, 2005
  4. JohnQ
    Replies:
    22
    Views:
    732
    JohnQ
    Jun 12, 2007
  5. Shan McArthur
    Replies:
    2
    Views:
    170
    Michael Baltic
    Jun 30, 2005
Loading...

Share This Page