Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 381345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:04:36+00:00 2026-05-12T15:04:36+00:00

I have a class, Fixture , that I want to cast to a 3rd

  • 0

I have a class, Fixture, that I want to cast to a 3rd party library class, b2FixtureDef. Currently, my function looks like this:

Fixture::operator b2FixtureDef() const {
    b2FixtureDef fd;
    fd.density = m_density;
    fd.friction = m_friction;
    fd.isSensor = m_isSensor;
    fd.restitution = m_restitution;
    fd.shape = &b2PolygonShape(m_polygon); // <-- the problem
    return fd;
}

Although this doesn’t produce any compile-time errors, I expect this will be a problem: fd.shape is a pointer. I’m casting my polygon object, to their polygon object, and then saving that location in memory. If I’m not mistaken, this will become immediately invalid after that line finishes executing.

So what’s a nice workaround for this? I could use the new operator, but my class shouldn’t really be allocating memory for some other class to clean up. I could actually store the b2PolygonShape in my class, but then I essentially have two copies of the same data (my polgon object, and their polygon object); I could get rid of mine, but it’s a lot easier to work with, which is why I created it in the first place. I could remove the entire function and make other classes responsible for doing the conversion themselves, but that wouldn’t be consistent with my other wrappers, and it isn’t a very nice solution. Is there any happy middle ground I’m overlooking?


Code now looks like this:

Fixture::operator b2FixtureDef() const {
    b2FixtureDef fd;
    fd.density = m_density;
    fd.friction = m_friction;
    fd.isSensor = m_isSensor;
    fd.restitution = m_restitution;
    fd.shape = new b2PolygonShape(m_polygon);
    return fd;
}

And gets used like this:

void Body::addFixture(const Fixture& fixture) {
    m_fixtures.append(fixture);
    b2FixtureDef fd = fixture;
    m_body->CreateFixture(&fd);
    delete fd.shape;
}

My goal was that I could just go m_body->CreateFixture(&fixture) and it would cast fixture to a b2FixtureDef and free up the resources by itself… but it doesn’t look like that’s possible with this stupid pointer dangling around.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:04:36+00:00Added an answer on May 12, 2026 at 3:04 pm

    A conversion operator always has to make a copy of everything, so that the result is valid even if the original object is freed. Which means that you need to make a copy of m_polygon as b2PolygonShape and assign a pointer to it to b2FixtureDef::shape.

    You can make a copy by implementing a copy constructor in b2PolygonShape or a conversion operator in whatever type m_polygon is. […]

    b2FixtureDef::shape looks like a has-a relation, so the destructor of b2FixtureDef should free it anyway, doesn’t it?


    Hmm, you say b2FixtureDef::shape is not freed automatically, what if you create a proxy class that has a b2FixtureDef as member and a conversion operator b2FixtureDef?

    When you create the proxy object you create b2PolygonShape on the heap and fill the b2FixtureDef members just like in your code. In the conversion operator you simply return the b2FixtureDef member and in the proxy destructor you free the b2PolygonShape:

    class FixtureProxy
    {
      b2FixtureDef m_Fixture;
      FixtureProxy(Fixture aFixture) {m_Fixture.Shape = new b2PolygonShape(aFixture.m_polygon);...}
      ~FixtureProxy() {delete m_Fixture.Shape}
      operator b2FixtureDef() const {return m_Fixture}
    }
    

    you then use it like this

    void Body::addFixture(const Fixture& fixture) {
        m_fixtures.append(fixture);
        m_body->CreateFixture(&b2FixtureDef(FixtureProxy(fixture)));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I encountered a class during my work that looks like this: public class MyObject
I am using Django. I have a Fixture model, which looks something like: class
I have class like this below shown. which contains the shopping items where the
I have an Entity class that exposes a read-only Id property. This property gets
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have a ManyToManyField that I want to present in a form, as a
I have a presenter class, that attaches an event of the injected view. Now
I have two jar files. MyProduct.jar (The business logic) MyProductFixture.jar. (The fixture that calls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.