Nicely done. +rep
Thread: casting |
Casting is when one variable is changed from one type to another, e.g. a floating point number to a integer value (3.5 to 3), or a cast of a class to another class.
There is the standard ways of casting a variable from one type to another.
This is the C way of doing things, and since there are things like classes then you want to make sure that the casting is done correctly, or you are told otherwise.Code:int value = (int)3.5;
There are 4 different types of casting
- static_cast
- dynamic_cast
- const_cast
- reinterpret_cast
I shall do each one in turn, so lets start with
Static_cast
Static casting is for casting between related types, int->float, int*->void* etc.
The basic syntax for a static_cast is
and a example would beCode:returnvariable = static_cast<return type>(casting value)
this would returned value would be 3, because there is no such thing as 0.5 in integer values.Code:float fl =3.5; int intFL = static_cast<int>(fl); // C similar code would be int intFL = (int)fl;
dynamic_cast
Dynamic casting is when you move from one class type to another that has polymorphic internals. When I say polymorphic, I basically mean that the there is a method that is a virtual method.
For example if you had a class like this
With a base class asCode:class baseClass { protected : int x; public : baseClass() { x = 55; }; virtual int returnX() { cout << "Base class" << endl; return x;} void setX(int newXValue) { x = newXValue;} };
The sub class overrides the base class returnX() function.Code:class subBaseClass : public baseClass { public : subBaseClass() : baseClass() {}; int returnX() { cout << "Subclass" << endl; return x;} };
So if you want to dynamically convert from one class to another you can, the good thing about dynamic casting is that it so some checks to make sure that things can "fit" as such and you can check against the returned value if the casting was done (check against 0 / NULL)
To cast between the two classes you can do something like this
This would convert the subBaseClass (sub) into a equivalent baseClass (base) type, but still holding onto any values that are within the subBaseClass for example.Code:subBaseClass *sub = new subBaseClass(); baseClass *base = dynamic_cast<baseClass*>(sub);
the setY and returnY and the value of y would still be "saved" in the dynamic_cast'ing of that variable into a "lesser" class, but if you cast'd back to a subBaseClass then you will have back the values of "y" in this case, because it does not effect the memory of the variable only allows for baseClass functions to act on that variable, this is good and also a reason would be if you had a function that did take in a class (baseClass in this example) as a parameter and then did some "work" on it and then returned back the class (baseClass), so you could still use this function if you casted back to a baseClass and once returned from the function you can then convert back to the sub class (subBaseClass).Code:class subBaseClass : public baseClass { private : int y; public : subBaseClass() : baseClass() {}; int returnX() { cout << "Subclass" << endl; return x;} void setY(int v) { y = v;} int returnY() { return y; } };
const_cast
Constant casting is basically taking the const (constant) restriction from a variable. If you have a constant value of pi, which makes sense.
But would like to have another value of pi ?? like to 3 decimal places you cannot doCode:const float pi = 3.14;
because it is a constant value, but you could take that value and add the extra bit to itCode:pi = pi + 0.002;
reinterpret_castCode:float newpi = const_cast<float>(pi); newpi+= 0.002;
The reinterpret casting is abit less type checking safe, compared to the dynamic_cast above, it only checks to see if the casting value can fit into the returning type bit patterns, so that you can convert from different variables but you are never sure if it will work or not and there is no real type checking either. Probably best to use a reinterpret_cast as a last resort really.
A good example is to convert a variable to a void pointer which can then be passed to a function, where the function could try and convert that void pointer to different classes and do some "work" on them. But a smaller example is to convert from a int pointer to a void pointer and then back again.
This will work fine, because we are just converting from a int to a void back to a int again, if we tried to convert to a float, there is no type checking so it for exampleCode:#include <iostream> int main() { int *aInt = new int(10); cout << "A value = " << *aInt << endl; void *bVoid = reinterpret_cast<void*>(aInt); int *aBack = reinterpret_cast<int*>(bVoid); cout << "COME BACK TO ME !! A value again = " << *aBack << endl; }
would work, but the aFloat would equal a weird number.Code:int *aInt = new int(10); cout << "A value = " << *aInt << endl; void *bVoid = reinterpret_cast<void*>(aInt); int *aBack = reinterpret_cast<int*>(bVoid); float *aFloat = reinterpret_cast<float*>(bVoid); cout << "float : "<< *aFloat << endl;
I do really like to have any feedback regarding any tutorial, just reply or PM me.. glad to help, better to share knowledge.
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
Nicely done. +rep
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Great tut +rep!
Just to say that if you are going from a sub class to the base class then you can do .
because it is not "adding" anything to the base.. But I do prefer to use the dynamic_cast when ever casting from one class to another because it has type checking and throws errors.Code:baseClass *base = 0; subClass *sub = new subClass(); base = sub;
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
For const_cast, it can be used to remove cv-qualifying type; which is to say it will also remove volatile, for example.
Your example is rather silly. Consider:
No cast needed.Code:const float pi = 3.14; float newpi = pi; newpi += 0.002;
Perhaps a better example would be one that with an object passed to a function via a const-qualified pointer, but the function needs to remove the const qualifier for whatever reason.
An example that comes to mind would be writing a function similar to C's strstr function: the strings passed should not be modified by the function and should therefore be const-qualified. But the returned pointer need not be const, even though it may point to a position in the string which was passed as const-qualified.
I was just trying to use a basic example to show what const_cast does..
Just a small test, I would something like strstr would be something like.. the code could be shortened abit more..
So the const_cast is when the return value is set to point to the rest of the string attached to the first pointer.
Code:#include <iostream> #include <string.h> using namespace std; char* codecallStrStr(const char* p1, const char* p2) { bool found; // loop through the first string while (*p1) { // if there is a match between the frist string character and the second string character if (*p2 == *p1) { if (strlen(p2) <= strlen(p1)) { found = true; for (int i =0; i < strlen(p2); i++) { if (p2[i] != p1[i]) { found = false; break; } } if (found) { return const_cast<char*>(p1); } } } p1++; } return 0; } int main() { char *searchStr = "hi thre there k "; char *pr = codecallStrStr(searchStr, "there"); // check to make sure it was found. if (pr) { cout << pr << endl; } else cout << "no found" << endl; }
Last edited by genux; 02-11-2010 at 02:57 AM. Reason: cleaned up the code abit.
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
Sorry that I am a nitpicker:
searchStr should be const because it points to a string literal -- which should be considered non-modifiable. Either that, or you need to make it into an array.Code:char *searchStr = "hi thre there k "; char *pr = codecallStrStr(searchStr, "there");
[edit]And...Code:char searchStr[] = "hi thre there k "; char *pr = codecallStrStr(searchStr, "there");
Yes. It's just that your example didn't show what const_cast does, which makes it a poor example.
Nps.. it is better to give a better example that would probably make more sense to a learner than a full blown one as such.Yes. It's just that your example didn't show what const_cast does, which makes it a poor example.
Is C++ your fav language ?
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
I was going with the theory of casting and not the full here is 100 lines of code.
In thinking about it, I probably should have done a cast'ing tutorial for each one, e.g. const_cast, dynamic_cast etc than just one for them all, so then I could have added at the bottom a coding example of each.
This is one of the reasons why I like doing tutorials/passing on knowledge as such, because what is the point in hiding it to yourself !! and also I get to learn more about what people like in tutorials and probably may learn more about the topic in question.
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
There are currently 1 users browsing this thread. (0 members and 1 guests)