Circular references involving internal classes

D

Dave Rudolf

Hey all,

I have a philosophical question for you folks -- that is, I don't really
need an immediate solution, but rather I am just curious.

Is there some way to make forward declarations for internal classes. The
situlation that I have is probably best illustrated with some code:


// legal forward declaration
class B;


// illegal forward declaration
// class B::Bi;

class A
{
B _b;
// B::Bi _bi;
};


class B
{
class Bi
{
};

A _a;
};


Let's forget that the above code is probably not the best design. Now, if I
want class A to handle references to class B, I can do the normal forward
declaration thing before class A, as above. But what if I want to reference
B's inner class, as suggested by the commented-out code? Is there any way to
do such a thing?

Of course, the work-around could be to move the inner class into it's own
class. Like I said, I'm just curious.

Dave
 
C

Chris \( Val \)

| Hey all,
|
| I have a philosophical question for you folks -- that is, I don't really
| need an immediate solution, but rather I am just curious.
|
| Is there some way to make forward declarations for internal classes. The
| situlation that I have is probably best illustrated with some code:
|
|
| // legal forward declaration
| class B;
|
|
| // illegal forward declaration
| // class B::Bi;
|
| class A
| {
| B _b;
| // B::Bi _bi;
| };
|
|
| class B
| {
| class Bi
| {
| };
|
| A _a;
| };
|
|
| Let's forget that the above code is probably not the best design. Now, if I
| want class A to handle references to class B, I can do the normal forward
| declaration thing before class A, as above. But what if I want to reference
| B's inner class, as suggested by the commented-out code? Is there any way to
| do such a thing?
|
| Of course, the work-around could be to move the inner class into it's own
| class. Like I said, I'm just curious.

Use a pointer to the objects:

class B;
class Bi;

class A
{
B* _b;
Bi* _bi;
};

Cheers.
Chris Val
 
R

Rob Williscroft

Dave Rudolf wrote in
Hey all,

I have a philosophical question for you folks -- that is, I don't
really need an immediate solution, but rather I am just curious.

Is there some way to make forward declarations for internal classes.

No, though you can do this:

class Outer
{
class Inner; /* kinda forward declare */
};

class Outer::Inner /* define */
{
};

The situlation that I have is probably best illustrated with some
code:


// legal forward declaration
class B;


// illegal forward declaration
// class B::Bi;

class A
{

This is illegal too, B is an *incomplete* type, you can declare
pointers and references to B (B*, B& etc) but not B's.
B _b;
// B::Bi _bi;
};
[snip]

Rob.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top