<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>cyclic dependencies</title>
		<link>http://www.allegro.cc/forums/view/234247</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 18 Jan 2003 02:55:10 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>how can i solve this problem:</p><p>//file1.cpp</p><p>class a<br />{<br />   public:<br />   void do_something_with_b();<br />};</p><p>void a::do_something_with_b()<br />{<br />   b.do_something();<br />}</p><p>//file2.cpp</p><p>class b<br />{<br />   public:<br />   void do_something_with_a();<br />};</p><p>void b::do_something_with_a()<br />{<br />   a.do_something();<br />}
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Thu, 16 Jan 2003 02:55:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>put the class declaration in thier own .h file, and include then where needed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 16 Jan 2003 03:08:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you sure you&#039;re not confusing class declarations and objects? A class is a template for an object, not the object itself. To use it properly, do like this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> Foo
<span class="k2">{</span>
 public:

 <span class="k1">int</span> var<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>

Foo bar<span class="k2">;</span>
bar.var <span class="k3">=</span> <span class="n">2</span><span class="k2">;</span>
</pre></div></div><p>

 Just to make sure, though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc ( mEmO)</author>
		<pubDate>Thu, 16 Jan 2003 14:10:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There&#039;s No way to protect against this kind of &#039;logical bug&#039;</p><p>Simply don&#039;t write the program in this way.</p><p>It&#039;s the same thing as writing:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> do_something<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span>
   do_something<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

It&#039;s a recursive situation without any exit condition.</p><p>If you want to make sure each one is only called once (I&#039;m not sure what you&#039;re trying to do), you have to perform a check to see that it doesn&#039;t continue forever - maybe pass along an integer counter in the call, increment it for each call, and see if it exceeds some max value for recursive calls?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Torbjörn Josefsson)</author>
		<pubDate>Thu, 16 Jan 2003 15:11:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>perhaps the example i gave is not very well<br />but i found a solution for this:<br />just put all header files of the classes in one big header file and include this file in every sourcefile so that each class knows the others
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Thu, 16 Jan 2003 15:50:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, you&#039;re problem was not one of functions calling each other, but one where the first class didn&#039;t know the definition of the other? Don&#039;t forget that you can use forward defintions, like :</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> Elephant<span class="k2">;</span>

<span class="k1">class</span> Mouse
<span class="k2">{</span>
   ...
   Elephant <span class="k3">*</span>ScaredCreature<span class="k2">;</span>
   ...
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">class</span> Elephant
<span class="k2">{</span>
   ...
   Mouse <span class="k3">*</span>ScaryCreature<span class="k2">;</span>
   ...
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

i.e. if the compiler really only needs to know that Elephant exists as a class (rather than being fussed about members), then that is all you need to tell it.</p><p>You know how I found that out? I had a little hunchback at the office.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Thu, 16 Jan 2003 17:23:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>yes with a pointer to a class this may work but when you declare a variable of that class it doesn&#039;t<br />like this:</p><p>class Elephant;</p><p>class Mouse<br />{<br />   ...<br />   Elephant ScaredCreature;<br />   ...<br />};</p><p>class Elephant<br />{<br />   ...<br />   Mouse ScaryCreature;<br />   ...<br />};
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Fri, 17 Jan 2003 01:06:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="quote_container"><div class="title">Frank Drebin said:</div><div class="quote"><p>yes with a pointer to a class this may work but when you declare a variable of that class it doesn&#039;t</p></div></div><p>

There&#039;s a good reason for that. For one thing, each of the resulting classes would be infinitely large, as they contain each other. <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Fri, 17 Jan 2003 01:12:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>so how can i break a dependencie like that:<br />the class player needs to know the class bullet (and its firing method) and the bullet needs  to know about the player (for collision detection)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Fri, 17 Jan 2003 01:26:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Obviously, your only chance is to use pointers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Fri, 17 Jan 2003 01:31:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you need A to know about B and B to know about A you CANNOT have A contain B and B contain A.  That makes no sense and it would go on forever.</p><p>You must use pointers or references and you must use forward delcarations as was shown to you earlier.  Do not place all classes in one big header or whatever you were considering.  In your class header, instead of doing #include &quot;Class.h&quot;, replace it with class Class; as much as possible.  As long as the class you are defining does not inherit from Class, does not contain a Class object, and does not have any methods taking a copy of a Class object (ie only Class* and Class&amp; are seen), you can and should use forward declarations.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Fri, 17 Jan 2003 01:32:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>And of course you should use include guards (or whatever they&#039;re called):</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">//file Enemy.h</span></td></tr><tr><td class="number">2</td><td><span class="p">#ifndef   ENEMY_H</span></td></tr><tr><td class="number">3</td><td><span class="p">#define   ENEMY_H</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="k1">class</span> Bullet<span class="k2">;</span></td></tr><tr><td class="number">6</td><td><span class="k1">class</span> Enemy <span class="k2">{</span></td></tr><tr><td class="number">7</td><td>    protected:</td></tr><tr><td class="number">8</td><td>        list <span class="k3">&lt;</span>Bullet <span class="k3">*</span><span class="k3">&gt;</span> bullets<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td>    public:</td></tr><tr><td class="number">11</td><td>        <span class="c">// whatever</span></td></tr><tr><td class="number">12</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td><span class="p">#endif   //ENEMY_H</span></td></tr><tr><td class="number">15</td><td>&#160;</td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>&#160;</td></tr><tr><td class="number">18</td><td><span class="c">//file Bullet.h</span></td></tr><tr><td class="number">19</td><td><span class="p">#ifndef   BULLET_H</span></td></tr><tr><td class="number">20</td><td><span class="p">#define   BULLET_H</span></td></tr><tr><td class="number">21</td><td>&#160;</td></tr><tr><td class="number">22</td><td><span class="k1">class</span> Enemy<span class="k2">;</span></td></tr><tr><td class="number">23</td><td><span class="k1">class</span> Bullet <span class="k2">{</span></td></tr><tr><td class="number">24</td><td>    protected:</td></tr><tr><td class="number">25</td><td>        Enemy <span class="k3">*</span>parent<span class="k2">;</span></td></tr><tr><td class="number">26</td><td>&#160;</td></tr><tr><td class="number">27</td><td>    public:</td></tr><tr><td class="number">28</td><td>        <span class="c">// whatever</span></td></tr><tr><td class="number">29</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>&#160;</td></tr><tr><td class="number">31</td><td><span class="p">#endif   //BULLET_H</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Fri, 17 Jan 2003 01:40:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>o.k. but i&#039;m not very good in pointers, so when i just declare a pointer to the bullet class in the player class how can i call then the bullet.fireit() method <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Fri, 17 Jan 2003 17:45:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>o.k. but i&#039;m not very good in pointers, so when i just declare a pointer to the bullet class in the player class how can i call then the bullet.fireit() method</p></div></div><p>Well, first you need to create a bullet (using <b>new</b>), but I&#039;m sure you knew that. Then you can call<br />bullet-&gt;fireit().
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Fri, 17 Jan 2003 17:59:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1. so i can use <br />   &quot;pointer_to_bullet=new bullet;&quot; and<br />   &quot;pointer_to_bullet-&gt;fireit();&quot;<br />   in the player.cpp code without having declared <br />   the class bullet before<br />   (just forward declaration like &quot;class<br />   bullet;&quot;) <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>2. and when theres a global object bullet1<br />   i can use<br />   &quot;pointer_to_bullet=&amp;bullet1;&quot; and<br />   &quot;pointer_to_bullet-&gt;fireit();&quot;<br />   ... <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Fri, 17 Jan 2003 18:34:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
1. No. You need to have declared the class, not just a forward declaration (I call these <i>class prototypes</i> as they are like function prototypes. I may be the only person that does that, though <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />). If you don&#039;t declare the class, how is it going to know that it has a member function called fireit()?</p><p>You can organize it like this, just as an example:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">class</span> Bullet</td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>   <span class="k1">void</span> fireit<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td>Bullet<span class="k3">*</span> somefunction<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">7</td><td><span class="k2">{</span></td></tr><tr><td class="number">8</td><td>   Bullet<span class="k3">*</span> b <span class="k3">=</span> <span class="k1">new</span> Bullet<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>   b-&gt;fireit<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>   <span class="k1">return</span> b<span class="k2">;</span></td></tr><tr><td class="number">11</td><td><span class="k2">}</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><span class="k1">void</span> Bullet::fireit<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">14</td><td><span class="k2">{</span></td></tr><tr><td class="number">15</td><td>   <span class="c">// whatever</span></td></tr><tr><td class="number">16</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
You can define fireit() (and all of Bullet&#039;s other functions) in a seperate file if you want. But if you want to access Bullet&#039;s members, you must declare the members first.</p><p>2. Sounds right ....
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sat, 18 Jan 2003 02:23:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="quote_container"><div class="title">Creepy Disembodied Bearded Tycho Head said:</div><div class="quote"><p>I call these class prototypes</p></div></div><p>

You should call them <i>classes</i>, simple enough. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /><br />Once you instantiate it, it&#039;s an object. Class is to object what prototype is to function, one could say. I think. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sat, 18 Jan 2003 02:39:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Nonono ...</p><div class="source-code snippet"><div class="inner"><pre>   <span class="c">// class prototype</span>
   <span class="k1">class</span> Bullet<span class="k2">;</span>
</pre></div></div><p>



</p><div class="source-code snippet"><div class="inner"><pre>   <span class="c">// class declaration</span>
   <span class="k1">class</span> Bullet
   <span class="k2">{</span>
      public:
         Bullet<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         ~Bullet<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         <span class="k1">void</span> fireit<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
   <span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>



</p><div class="source-code snippet"><div class="inner"><pre>   <span class="c">// class definition</span>
   Bullet::Bullet<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span>
      <span class="c">// blah blah blah</span>
   <span class="k2">}</span>

   Bullet::~Bullet<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span>
      <span class="c">// blah blah blah</span>
   <span class="k2">}</span>
</pre></div></div><div class="source-code snippet"><div class="inner"><pre>
   <span class="k1">void</span> Bullet::fireit<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span>
      <span class="c">// blah blah blah</span>
   <span class="k2">}</span>
</pre></div></div><p>
Geddit?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sat, 18 Jan 2003 02:42:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
How interesting, then, that there are no such in the code snippet. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sat, 18 Jan 2003 02:43:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Huh? Look at Frank&#039;s last post. I know I didn&#039;t use one; it wouldn&#039;t have worked. That was the point <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sat, 18 Jan 2003 02:46:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Just call them forward declarations like everyone else, all right? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sat, 18 Jan 2003 02:48:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
I called one thing a class prototype, and another thing a class declaration. Which one is the forward declaration, can we call function prototypes that, and what do you call the other class thing (whichever isn&#039;t a declaration)?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sat, 18 Jan 2003 02:52:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
s/class prototype/forward declaration of class x/
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sat, 18 Jan 2003 02:55:10 +0000</pubDate>
	</item>
</rss>
