forward class declaration and circular including

B

blueblueblue2005

Hi, there was a post several days ago about using forward class
declaration to solve the circular including issue, today, one question
suddenly came into mind: which class should the forward class
declaration statement be in? suppose I have a class A and B, sample
code as following:

// A.h
#ifndef A_H
#define A_H

class B; // forward declaration

// define A here

#endif

// B.h
#ifndef B_H
#define B_H

#include B.h

// define B here

#endif

from the ListNode and List example in Deital book, I found if A is part
of B, say,ListNode is part of List, then the forward declaration
statement is in A. Is this the general rule? or we can randomly put the
forward declaration stmt in either class, and let the other to use the
#include ???
 
M

msalters

blueblueblue2005 schreef:
Hi, there was a post several days ago about using forward class
declaration to solve the circular including issue, today, one question
suddenly came into mind: which class should the forward class
declaration statement be in?

Both. The guideline is to use a forward declaration when possible,
and only use the #include if needed.

HTH,
Michiel Salters
 
B

blueblueblue2005

Both. The guideline is to use a forward declaration when possible,
and only use the #include if needed.

I tried to switch the classes which have the forward declaration and
#include stmt, it turned out that it works(no compiling error) only
when A has the forward declaration and B has the #include stmt. so not
Both, which will make a circular forward declaration.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top