Simple Class Question

Discussion in 'C++' started by Brian C, Jul 4, 2006.

  1. Brian C

    Brian C Guest

    Hello all,
    I have, what I'm sure is an idiotic question, but I can't seem to find
    an answer for it (probably looking in the wrong direction of course).
    I'm coming from numerous years in C programming, trying to expand my
    horizons and get into C++.
    I did a little bit of Java programming along my C days, and this
    problem was solved with getParent(), which doesn't pertain to C++ I've read.
    So, if I have this:

    ......(file: A.h).....
    #ifndef __A_H
    #define __A_H
    #include "b.h"
    class A
    {
    public:
    ...
    private:
    B *ClassBPtr;
    };
    #endif


    .....(file: B.h).....
    #ifndef __B_H
    #define __B_H
    #include "a.h"
    class B
    {
    public:
    ...
    private:
    A *ClassAPtr;
    };
    #endif


    I of course get an error on compiling because when compiling "b.h", it
    cannot figure out what class "A" is.

    The basic idea is that, lets say class B is a thread running
    independently of the class A instance. Some data comes in, and it needs
    to feed it to class A. I know it's not a perfect example, but it's
    probably the simplest I can come up with right now.

    I know that I could do a nested class with class B inside of class A,
    but what if I need to use class B somewhere else?

    Thanks in advance.
     
    Brian C, Jul 4, 2006
    #1
    1. Advertisements

  2. Brian C posted:


    What would you do in C if you had two functions that called each other...
    ? You'd forward-declare one of them.

    void Func1(void);

    void Func2(void)
    {
    Func1();
    }

    void Func1(void)
    {
    Func2();
    }


    ......(file: A.h).....
    #ifndef __A_H
    #define __A_H
    #include "b.h"

    class B; /* Forward Declaration */

    class A
    {
    public:
    ...
    private:
    B *ClassBPtr;
    };
    #endif


    .....(file: B.h).....
    #ifndef __B_H
    #define __B_H
    #include "a.h"

    class A; /* Forward Declaration */

    class B
    {
    public:
    ...
    private:
    A *ClassAPtr;
    };
    #endif
     
    Frederick Gotham, Jul 4, 2006
    #2
    1. Advertisements

  3. Brian C

    red floyd Guest

    You don't need this #include in the h file.
    You don't need this #include in the h file.
    To summarize:

    A.h:

    class B;
    class A {
    // ... redacted
    B* bPtr;
    };


    B.h:

    class A;
    class B {
    // redacted
    A* aPtr;
    };


    Further, your include guards cause your program to be ill formed. Any
    identifier with two consecutive underscores is reserved to the
    implementation. Similarly, any identifier with a leading underscore
    followed by an uppercase letter is also reserved. In addition, any
    identifier with a leading underscore is reserved in the global scope and
    in namespace std.
     
    red floyd, Jul 4, 2006
    #3
  4. Brian C

    red floyd Guest

    Even better, see the FAQ. In particular 39.11:
    http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11
     
    red floyd, Jul 4, 2006
    #4
  5. Brian C

    Brian C Guest

    :::snip:::

    Ok, definitely thank you all very much for the EXTREMELY timely
    response. I was not aware that I did not have to include the other
    header file to each respective class.

    As far as my "__"'s good to know. I never ran into a problem with it in
    C, but I will change my notation. Thank you. I don't know where I picked
    it up, been doing it for years =).
     
    Brian C, Jul 4, 2006
    #5
  6. Brian C

    Brian C Guest

    Frederick Gotham wrote:
    You're right, I'd just do an extern, etc. I do wonder though, I looked
    through one of my C++ books, and they do not mention forward
    declaration. I've got two more C++ books at work, doubt they mention it
    either.
    It seems to me to be such a basic thing, so surprised.
     
    Brian C, Jul 4, 2006
    #6
  7. Brian C posted:

    You'll learn more on this newsgroup than you will from any book (unless you
    have one of those interactive books! ; ).
     
    Frederick Gotham, Jul 4, 2006
    #7
  8. Brian C

    osmium Guest

    Clearly you have not read the printed version of _C++ FAQs_.
     
    osmium, Jul 4, 2006
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.