Hi all,

My first post here -- I've been trying to update some old code that uses C-style casts into the C++ equivalent, and I've stumbled on one cast that I can't figure out how to convert.

This isn't the code that was giving me issues, but it reproduces the problem:

Old code would look like this:

short g_myshort;

const short& someFunction()
{
  return g_myshort;
}

int main()
{
  g_myint = 5;
  int* pnum = (int*)(&someFunction());
}

I'd like to replace that last call with something like:

int* pnum = static_cast<int*>(&someFunction());

or possibly

const int* pnum = static_cast<const int*>(&someFunction());

but in both cases, the compiler yells: cannot convert from 'const short *__w64' to int */const int*.

I've tried nesting a const_cast inside the static_cast, and vice versa, but neither has worked. Any ideas? Or am I forced to stick to the C style cast here.

And yes, I realize I could store the return value and get it done that way, but I'm interested from a theory angle on why the C-style cast can get it done in one line and the C++-style seemingly can't.

Thanks!

short* pnum = static_cast<short*>(&const_cast<short&>(someFunction())); or this const short* pnum = static_cast<const short*>(&someFunction());

Edited 7 Years Ago by Ancient Dragon: n/a

short* pnum = static_cast<short*>(&const_cast<short&>(someFunction())); or this const short* pnum = static_cast<const short*>(&someFunction());

Well, this leaves the variable as a short. Testing further, it looks like the simplest reduction of the problem is the casting of pointers:

short myShort = 0;
int iTest = static_cast< int >( myShort );

works, but

short* pmyShort = &myShort;
int* piTest = static_cast< int* >( pmyShort )

does not. I guess I don't understand why?

And what's wrong with leaving it as a short pointer? Casting it as an int pointer will not give it any more precision.

I was hoping to concentrate more on the problem than the specifics used in the example. The actual pointers involved are all user classes in an inheritance hierarchy -- short and int were just used for convenience.

maybe you need a better example that describes more accurately what you are trying to do. Maybe something like this???

class base
{
 // blabla
};

class derived : public base
{
  // blabla
};

void foo( base* pBase)
{
   derived* pDerived = reinterpret_cast<derived*>(pBase);
}

Thanks for sticking with it. Anyhow, I think what you're recommending -- reinterpret_cast -- will work, however, I've always been told its like a last resort. From MSDN: "The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type. Other uses are, at best, nonportable." I actually am, in this case, converting a child class back to its parent class so that a function can use it as an argument. Since the parent pointer supports all the operations the child does, I guess I'm just confused why I can't static cast that.

maybe you need a better example that describes more accurately what you are trying to do. Maybe something like this???

class base
{
 // blabla
};

class derived : public base
{
  // blabla
};

void foo( base* pBase)
{
   derived* pDerived = reinterpret_cast<derived*>(pBase);
}
This article has been dead for over six months. Start a new discussion instead.