[SOLVED]static_cast is giving a C2440

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

[SOLVED]static_cast is giving a C2440

Post by LJS »

When adding a simple snippet my IDE gives the following error:

C2440 'conversion' : cannot convert from 'type1' to 'type2'

Code: Select all

btCollisionObject* obA = static_cast< btCollisionObject* >( contactManifold->getBody0() );
The defenition of contactManifold

Code: Select all

class btPersistentManifold
{
  const btCollisionObject* m_body0;
  SIMD_FORCE_INLINE const btCollisionObject* getBody0() const { return m_body0;}
}
Possible solution:

Code: Select all

btCollisionObject* obA = (btCollisionObject*)contactManifold->getBody0();
Why do I get the error and what is the difference/the same with this solution?


Regards,
Last edited by LJS on Mon Feb 04, 2013 3:39 pm, edited 1 time in total.
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: static_cast is giving a C2440

Post by bstone »

The const modifier. Change to:

Code: Select all

const btCollisionObject* obA = static_cast< const btCollisionObject* >( contactManifold->getBody0() );
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: static_cast is giving a C2440

Post by LJS »

As if we were chatting, as fast as the answer came, thanks.

But then the static_cast is not needed at all.

The snippet is the well pasted snippet out of the manual of Bullet. Nobody is mentioning this anywhere. Therefore I think there must be some setting effecting it (VC2010)
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: static_cast is giving a C2440

Post by bstone »

You're correct, the static cast is not needed, but the problem is the const modifier. C++ doesn't let you remove it with a static_cast<>, you have to use a const_cast<> instead but that signals some issues with your code design choices more often than not. It's not VC2010, it's the C++ standard and the deep thinking behind adding the const mechanics to the language.

It could be that the authors of the original code compiled it only with GCC using some forgiving options, if you say that's not a typo somewhere.
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: static_cast is giving a C2440

Post by LJS »

Think calling it after the bullet pipeline it should be save to change the instance and so I propably agree the const is there. But then again, it's almost right before the pipeline too XD.

Thanks tbone.

I'll leave it still unsolved too see more feedback.

Regards
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: static_cast is giving a C2440

Post by Kojack »

A quick google shows that apparently the const was added to bullet 2.81 (latest version, from october last year). So all the code you've seen that does the static cast is out of date.
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: static_cast is giving a C2440

Post by LJS »

Admitted, I am the worsed at search engines. Since algorithms are trying to help me find something I can't find anything anymore. Always using different queries and getting the same results.

Took the example from their wiki, which is not updated.

Thanks for the response Kojack.

Kind regards,
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: [SOLVED]static_cast is giving a C2440

Post by Kojack »

I wasn't saying that you should have searched better, just that I didn't spend much time looking into how truthful the claim was. :)
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: [SOLVED]static_cast is giving a C2440

Post by LJS »

Didn't thought you were, just expressing my frustrations about the searchengines. Should have been more clear myself.

Anyway, I tried to find it too. What query did you put in? Maybe I'll get better at it.
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: [SOLVED]static_cast is giving a C2440

Post by Kojack »

What query did you put in?
I did a search for:
static_cast< btCollisionObject* >
The first link in google is a thread on the bullet forum where a guy had the same problem, then discovered that 2.81 changed constness of some members.
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: [SOLVED]static_cast is giving a C2440

Post by LJS »

Wanna learn how to use a search engine, check Ogre. :D

Also funny to see he came up with the same solution, good for convidence.

Regards,
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static