the design of classes referencing each other in the large c++ project

P

puzzlecracker

Hello Group,


Say I have classes A and B and the both need to maintain a pointer/
reference to each for various reasons, say optimization. And there are
other classes in the project use these two classes, among others.
What is the best design for such architecture?


I saw errors by explicitly including headers of both classes. Also,
assume that include guards are included in each header file. I prefix
each class name with C to preserve convention.

Also, instead of including headers directly, I try to maintain
pointers to each class for now. But below is what I am trying to
create in best way, least coupling, and NO COMPILER ERRORS

In CA.h file
----------------
include"B.h"
//and other headers

class A {
private:
B * pB;
};
------------------

In CA.cpp
-----------------
#include "CA.h"
//other headers
// implementation of A class memebers


In CB.h file
----------------
include"A.h"
//and other headers

class B {
private:
A * pA;
};
------------------

In CB.cpp
-----------------
#include "CB.h"
//other headers
// implementation of A class members


COther.h

#include "CA.h"
#include "CB.h"
//some other stuff

COther.cpp

#include<COther.h>

//implementation
====================


Please let me know if my question isn't clear enough, I am working
with a huge system and want to organize one of its component classes
in the best fashion possible, avoiding coupling and a long build
time.

Other than Lakos's classic (Large scale C++ software design), what
reading material (books, papers) would you suggest dealing explicitly
with large projects and mentioned sort of issues?

Thanks
 
E

Erik Wikström

Hello Group,


Say I have classes A and B and the both need to maintain a pointer/
reference to each for various reasons, say optimization. And there are
other classes in the project use these two classes, among others.
What is the best design for such architecture?


I saw errors by explicitly including headers of both classes. Also,
assume that include guards are included in each header file. I prefix
each class name with C to preserve convention.

Also, instead of including headers directly, I try to maintain
pointers to each class for now. But below is what I am trying to
create in best way, least coupling, and NO COMPILER ERRORS

You shouldn't have to include B's header in A's header, what you need is
forward declarations.
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top