accessing functions from other objects

Pages: 12
1
2
3
4
5
class Soc; //not need to #include
class ModCicComb
{
	Soc* pSoc;
}

- do nothing if: A makes no references at all to B
- do nothing if: The only reference to B is in a friend declaration
- forward declare B if: A contains a B pointer or reference: B* myb;
- forward declare B if: one or more functions has a B object/pointer/reference
as a parementer, or as a return type: B MyFunction(B myb);
- #include "b.h" if: B is a parent class of A
- #include "b.h" if: A contains a B object: B myb;


Whatever the solution is, will be replicated in about 20 files (so mathhead's use of a parent class looks a little better now!).
Making the inheritance will imply to write : public Parent in every file. If the inheritance makes sense (because it is an "is a" relationship) then use it.
Just be careful :)
ne555: I need more than just a forward declaration. In my .cpp file, I access a member of *pSoc. I guess I can solve this by including the soc.h file directly in my modciccomb.cpp file. I was hoping to avoid this, and have source files only include their respective header files, as that seemed like a good idea. But I guess that won't work, will it?

AFAIK, The cpp need to #include "soc.h" because it uses it, but the .h can't include it because of the circular dependency.

and have source files only include their respective header files, as that seemed like a good idea
Not sure if that is a good idea. You will be putting headers that you don't actually need in the .h, adding artificial dependencies.
By instance (all headers are guarded)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//a.h
#include "c.h"
class b;
class a{
  ptr *b;
}

//a.cpp
#include "a.h"
void a::foo(){
  c C; //using the c header
}

//b.h
#include "a.h"
//... 

Now if you modify c.h, ¡you will need to recompile b.cpp!

Just put what you need, where you need it.
Last edited on
I see your point, and will review what I've written to eliminate the unnecessary includes.

I think this problem has been resolved. Many thanks to ne555 and MathHead for all the help!
Oops...one more thing: I forgot about initializing the back pointer. My first thought was to pass the pointer to the Soc at the construction of the component class, but...I don't think this will work like this:

1
2
3
4
class Soc
{
private:
   Comb comb (Soc* this);


Can someone please advise what the preferred way to do this is? Thanks.
What is the back pointer, and what is it for? Is Comb comb (Soc* this); a method, because this is a reserved keyword and can't be used as a method parameter (as far as I know.)
Is this what you were looking for:
1
2
3
4
5
6
7
class Soc {
 private:
   Comb comb;

 public:
   Soc(Comb *c) : comb(c); //or
};
The back pointer was so objects within Soc could get to other objects contained by the Soc class. Many of my objects need to get a datum from a certain other object, but they have no visibility to that object. My solution was to have a pointer to the "master" object (the Soc) from which they could get to the other object, like this:

1
2
3
4
5
6
		gain.cycle
				(stage.at(LAST_CIC_COMB_STAGE).getIOut(),
				 stage.at(LAST_CIC_COMB_STAGE).getQOut(),
				 pSoc->modHost.getValue(MOD_HOSTCICGAIN),
				 clockEnable,
				 resetFlag);


...referring to the 3rd parameter in this call.
I realize my above explanation wasn't too well worded. Let me try to simplify this:

Assume one object contains another object (of a different class). When the "outer" object is created, how can it pass its address to the "inner" object? This must be possible.

Thanks.
1
2
3
4
5
class Outer{
  Inner inner;
public:
  Outer(): inner(this){}
};
Not sure.

Edit:
As you can see the object is not fully construct yet, so it remains in an invalid state. The calls to its methods should be forbidden then.
Also if the relationship is always 1 to 1, then they are the same thing and no two separate classes.
Last edited on
I think that did it, ne555. I'll test in the AM and report back. Thanks!
Topic archived. No new replies allowed.
Pages: 12