By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

Simple Class Question

P: n/a
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.
Jul 3 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Brian C posted:

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

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 3 '06 #2

P: n/a
Frederick Gotham wrote:
Brian C posted:

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


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"
You don't need this #include in the h file.
>
class B; /* Forward Declaration */

class A
{
public:
...
private:
B *ClassBPtr;
};
#endif
....(file: B.h).....
#ifndef __B_H
#define __B_H
#include "a.h"
You don't need this #include in the h file.
>
class A; /* Forward Declaration */

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

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.
Jul 3 '06 #3

P: n/a
Brian C wrote:
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

Even better, see the FAQ. In particular 39.11:
http://www.parashift.com/c++-faq-lit...html#faq-39.11

Jul 3 '06 #4

P: n/a
Frederick Gotham wrote:
Brian C posted:

:::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 =).
Jul 4 '06 #5

P: n/a
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.
Jul 4 '06 #6

P: n/a
Brian C posted:
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.

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 '06 #7

P: n/a
"Frederick Gotham" writes:
You'll learn more on this newsgroup than you will from any book (unless
you
have one of those interactive books! ; ).
Clearly you have not read the printed version of _C++ FAQs_.
Jul 4 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.