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 3426564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:43:52+00:00 2026-05-18T06:43:52+00:00

I’m using the Chipmunk physics engine for my C++ project. Chipmunk has it’s own

  • 0

I’m using the Chipmunk physics engine for my C++ project.

Chipmunk has it’s own vector, cpVect, which is nothing but a struct { double x, y; };

However I have my own operator overloaded Vector class in my project – and I’d like to use it just as a cpVect and vice-versa, without any conversions
(my class Vector‘s only data fields are double x, y as well)

Currently I use this for implicit conversions (for argument passing, setting a Vector variable to a cpVect, etc.)

inline operator cpVect() { return cpv( x, y ); };
inline Vector( cpVect v ) : x( v.x ), y( v.y ) {}

This works for most things, but has a few problems – for example it isn’t compatible for using arrays of the type Vector with cpVect arrays.

For that I currently have to do this:

Vector arrayA[123];
cpVect* arrayB = (cpVect*) arrayA;
  • Is it possible by some way to use my Vector as cpVect and vice-versa? Data is identical with both structures, mine only has additional methods.
  • 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-18T06:43:53+00:00Added an answer on May 18, 2026 at 6:43 am

    So if I understand you, you have this:

    class Foo
    {
      double x, y;
    };
    
    class Bar
    {
      double x, y;
    };
    

    …and you want pretened that a Foo is actually a Bar, right?

    You can’t — not without evoking Undefined Behavior. No matter what you try, you will fall in to the great UB black hole.

    • if you try to use a union, you’ll evoke UB when you assign to my_union.foo and read from my_union.bar
    • if you try to reinterpret_cast you’ll evoke UB when to try to use the casted pointer
    • if you try to use a C-style cast, you’ll have the same problems as above

    The problem is this: Even though Foo and Bar look the same, they are not the same. As far as C++ is concerned, they are completely different types, totally unrelated to each other. This is a Good Thing, but it can also throw up some barriers that can seem to be artificial and arbitrary.

    So, in order to do this without evoking UB, you need to convert. You could make this nearly automatic, by simply adding a couple facilities to Bar (which I presume to be the one you wrote, not the one from the library):

    class Bar
    {
    public:
      Bar(const Foo& foo) : x(foo.x), y(foo.y) {}; // lets you convert from Foo to Bar
      Bar& operator=(const Foo& foo) { x = foo.x; y = foo.y; return * this; }
    
      operator Foo() const { Foo ret;  ret.x = x; ret.y = y; return ret; } // lets you convert ffrom Bar to Foo
    private: 
      double x, y;
    };
    

    Which will let you go from Foo to Bar and back again with little or no code. Plus the compiler will optimize a lot of this away in release builds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string

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.