Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen classes want to couple
    primarykey
    data
    text
    <p>I am having an issue with 2 classes that were once nicely separated, but now they want to couple.</p> <p>Without getting too much into the specifics of the problem, here it is:</p> <p>I used to have a class Triangle that contained 3 space-position vertices.</p> <pre><code>class Triangle { Vertex a,b,c ; // vertices a, b and c } ; </code></pre> <p>There were many Triangle instances in the program, so each had retained their own copy of their vertices. Member functions such as <code>getArea()</code>, <code>getCentroid()</code> etc were written in class <code>Triangle</code>, and since each <code>Triangle</code> instance had copies of Vertex a, b and c, finding the area or centroid had no dependence on other classes. As it should be!</p> <p>Then I wanted to move to a vertex-array/index buffer style representation, for other reasons. This means all vertices are stored in a single array located in a <code>Scene</code> object, and each <code>Triangle</code> retains only REFERENCES to the vertices in <code>Scene</code>, not copies of the vertices themselves. At first, I tried switching out for pointers:</p> <pre><code>class Scene { std::vector&lt;Vertex&gt; masterVertexList ; } ; class Triangle { Vertex *a,*b,*c ; // vertices a, b and c are pointers // into the Scene object's master vertex list } ; </code></pre> <p>(In case you're wondering about the benefits, I did it for reasons mostly to do with triangles that share vertices. If *a moves then all triangles that use that vertex are updated automatically).</p> <p>This would have been a really good solution! But it didn't work reliably, <a href="https://stackoverflow.com/questions/3287801/pointers-to-elements-of-stdvector-and-stdlist">because std::vector invalidates pointers</a>, and I was using a std::vector for the master vertex list in class <code>Scene</code>.</p> <p>So I had to use integers:</p> <pre><code>class Triangle { int a,b,c ; // integer index values // into the Scene object's master vertex list } ; </code></pre> <p>But now I have this new coupling problem: to find its own area or centroid, class <code>Triangle</code> needs access to <code>class Scene</code> where before it did not. It seems like I've fsck`d something up, but not really.</p> <p>WWYD?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload