Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to deal with object that can't instantiate itself?
    primarykey
    data
    text
    <p>I guess I've asked a few similar questions before, but I was beating around the bush. I think this is the real problem that I can't quite lay to rest.</p> <p>I'm dealing with a <a href="http://www.box2d.org/" rel="nofollow noreferrer">third party library</a>, and there's an object that can't create itself, <code>b2Body</code>. The <code>b2World</code> has to <a href="http://programanddesign.com/box2d/classb2World.html#2eb36e967e43294bfa03ec3d177c2dae" rel="nofollow noreferrer">instantiate it</a>. I personally don't like this design pattern very much; I think the <code>b2Body</code> should be able to exist independently of the world, and then be added to the world when needed. Anyway, I've wrapped <code>b2Body</code> with my own class class, <code>Body</code>, because I need to add some extra stuff to it anyway. Similarly, I have a <code>World</code> wrapper. Now I figure I have 3 options:</p> <ol> <li>Have <code>Body</code>'s constructor takes a pointer to <code>World</code> so that it can be fully instantiated (calls <code>b2World::CreateBody</code> somewhere inside) -- i.e. have a constructor like <code>Body *b = new Body(world_ptr)</code></li> <li>Pass <code>Body</code> to some <code>World::CreateBody</code> method like how the library already does it -- i.e. <code>Body *b = world.CreateBody(params);</code></li> <li>Duplicate all the data in <code>b2Body</code> so that you can use it however you want, and then after you add it to the world it will 'switch over' to use the <code>b2Body</code> data -- i.e. <code>Body b</code> and later <code>world.addBody(b)</code>.</li> </ol> <p>(1) and (2) mean that you can't have a <code>Body</code> without a <code>World</code>, which I probably won't need, but it might be nice to have that option [so that I can use it as a template for other objects and such]. Not sure what other pros and cons there are. (3) seems nicer, but it's a lot more work to implement, and it means I have to duplicate most of the data that's already contained in <code>b2Body</code>.</p> <p>What are your thoughts? I'll <code>CW</code> this just so no one frets.</p> <hr> <p>I still can't lay this to rest. This is what each of the options would look like:</p> <p><strong>Option 1:</strong> (what I prefer)</p> <pre><code>World w; Body b; Fixture f; b.addFixture(f); w.addBody(b); </code></pre> <p><strong>Option 2:</strong> (somewhere in the middle)</p> <pre><code>World w; Body b(w); Fixture f(b); </code></pre> <p><strong>Option 3:</strong> (how Box2D does it)</p> <pre><code>World *w = new World; Body *b = w-&gt;CreateBody(args); Fixture *f = b-&gt;CreateFixture(args); </code></pre> <p>Options 2 and 3 aren't so different, but it changes who has control over is creating the objects.</p> <p>How would I actually implement option 3 though? <code>World::CreateBody()</code> has to call <code>b2World::CreateBody(args)</code> which calls <code>b2Body::b2Body()</code> and returns <code>b2Body</code> but never calls <code>Body::Body(args)</code> which is a problem. The <code>b2Body</code> would get fully initialized, but my wrapper has no place to do it's thing... More specifically, how would I write <code>World::CreateBody(const BodyDef &amp;bd)</code>? Assuming BodyDef inherited from b2BodyDef, Body from b2Body, World from b2World, etc.</p>
    singulars
    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