Home > Learning > Cast away

Cast away

My C++ class is done, and I feel like I learned some tiny things here and there, mostly refresher stuff, but not a lot of meat to sink my teeth into.

So, to learn more, I turned to Stack Overflow, and while wanting to be helpful, got served. It’s alright, it’s cool, I learned I had still lots to learn. And that’s why I will talk about casting.

Usually, in a function, you should get your types right, and avoid wasteful casting. That’s true. Compilers throw warnings at you if you don’t respect types, and that’s usually where you need to be explicit about what you mean. C has the classic cast, what most call the C-style cast:

    int i = 35;
    float f = (float)i;

C++ offers a lot more tools. Most of the time, we’re casting to another type to adapt to a situation, a function, etc. C++ allows us to be more explicit, which is good, because you’re making your meaning clear to future maintainers of your code (and that can be yourself). Never neglect the power of explicit code. Don’t just cast to make it work (or not send a warning), cast and be clear about your intentions.

These are called casting operators, and they go as follows.

First, const_cast.

const_cast exists to deal with whether a variable or parameter is constant or not. Sometimes, you need to get rid of “const” for just a bit. Casting it C-style works, but the C++ way is crystal:

void nonconstfunc(int *i);

void constfunc(const int *i)
{
    nonconstfunc(i); //error, this will not compile
    int* i2 = const_cast<int*>(i); // but convert it to a non-const int pointer
    nonconstfunc(i2); //and use the non-const copy instead!
}

Next is reinterpret_cast.

reinterpret_cast does a very specific job: it can cast a pointer to an integral type and back. So if you have a pointer and you want to print its address, say, you can use this:

int main(int argc, char *argv[])
{
	long adr = reinterpret_cast<long>(argv);
	cout << adr;
	return 0;
}

It’s a very specific use. Changing the value and casting it back to pointer is definitely not recommended. You can see this being used to hash an address and put it in a hash table, for example.

The next two are opposite sides of the same coin: dynamic_cast and static_cast. I don’t fully grok them yet, so I will not elaborate on them in this post. Suffice it to say that they complement each other. dynamic_cast runs real-time checks, while static_cast does not, which makes it faster. Both can cast up or down a chain of inheritance. So I will grok and get back to this.

About these ads
Categories: Learning Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: