Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

casting


  • Please log in to reply
10 replies to this topic

#1 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 08 February 2010 - 04:05 AM

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.

int value = (int)3.5;

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.

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

returnvariable = static_cast<return type>(casting value)

and a example would be

float fl =3.5;
int intFL = static_cast<int>(fl);
// C similar code would be
int intFL = (int)fl;

this would returned value would be 3, because there is no such thing as 0.5 in integer values.

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

class baseClass
{
  protected : 
     int x;
  public : 
     baseClass()  { x = 55; };
     virtual int returnX() { cout << "Base class" << endl; return x;}
     void setX(int newXValue) { x = newXValue;}
};

With a base class as

class subBaseClass : public baseClass
{
  public : 
      subBaseClass() : baseClass() {};
      int returnX() { cout << "Subclass" << endl; return x;}
};

The sub class overrides the base class returnX() function.

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

subBaseClass *sub = new subBaseClass();
baseClass *base = dynamic_cast<baseClass*>(sub);

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.

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; }

};

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).

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.

const float pi = 3.14;

But would like to have another value of pi ?? like to 3 decimal places you cannot do

pi = pi + 0.002;

because it is a constant value, but you could take that value and add the extra bit to it

float newpi = const_cast<float>(pi);
newpi+= 0.002;

reinterpret_cast

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.

#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;
}

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 example

  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;

would work, but the aFloat would equal a weird number.

I do really like to have any feedback regarding any tutorial, just reply or PM me.. glad to help, better to share knowledge.
  • 3
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 08 February 2010 - 02:29 PM

Nicely done. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 08 February 2010 - 05:05 PM

Great tut +rep!
  • 0

#4 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 09 February 2010 - 03:50 AM

Just to say that if you are going from a sub class to the base class then you can do .
baseClass *base = 0;
subClass *sub = new subClass();

base = sub;

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.
  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#5 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 10 February 2010 - 12:10 PM

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:
const float pi = 3.14;
float newpi = pi;
newpi += 0.002;
No cast needed.

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.
  • 0

#6 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 11 February 2010 - 02:48 AM

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.

#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;
}

Edited by genux, 11 February 2010 - 02:57 AM.
cleaned up the code abit.

  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#7 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 11 February 2010 - 02:41 PM

Sorry that I am a nitpicker:
[COLOR="Red"]char *[/COLOR]searchStr = "hi thre there k ";
    char *pr = codecallStrStr(searchStr, "there");
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.
char searchStr[] = "hi thre there k ";
    char *pr = codecallStrStr(searchStr, "there");

[edit]And...

I was just trying to use a basic example to show what const_cast does..

Yes. It's just that your example didn't show what const_cast does, which makes it a poor example.
  • 0

#8 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 12 February 2010 - 01:53 AM

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.

Is C++ your fav language ?
  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#9 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 12 February 2010 - 03:16 PM

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.

If you've got one, replace your non-example with it then.

Is C++ your fav language ?

Nope.
  • 0

#10 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 13 February 2010 - 12:25 AM

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.
  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#11 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 13 February 2010 - 12:30 AM

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.


I don't think you're getting what dcs is saying.

He is trying to tell you that your example is bad, not for the simplicity, but because the const_cast doesn't do anything.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download