Simple Class Question

B

Brian C

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

Frederick Gotham

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
 
R

red floyd

Frederick said:
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"
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.
 
R

red floyd

Brian said:
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-lite/misc-technical-issues.html#faq-39.11
 
B

Brian C

Frederick said:
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 =).
 
B

Brian C

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

Frederick Gotham

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! ; ).
 
O

osmium

Frederick Gotham said:
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_.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top