+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: casting

  1. #1
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    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.

    Code:
    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
    1. static_cast
    2. dynamic_cast
    3. const_cast
    4. 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

    Code:
    returnvariable = static_cast<return type>(casting value)
    and a example would be

    Code:
    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

    Code:
    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

    Code:
    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

    Code:
    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.

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

    Code:
    const float pi = 3.14;
    But would like to have another value of pi ?? like to 3 decimal places you cannot do

    Code:
    pi = pi + 0.002;
    because it is a constant value, but you could take that value and add the extra bit to it

    Code:
    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.

    Code:
    #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

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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: casting

    Nicely done. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Nov 2009
    Location
    London
    Posts
    866
    Blog Entries
    3
    Rep Power
    13

    Re: casting

    Great tut +rep!

  5. #4
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: casting

    Just to say that if you are going from a sub class to the base class then you can do .
    Code:
    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.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  6. #5
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: casting

    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:
    Code:
    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.

  7. #6
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: casting

    Quote Originally Posted by dcs View Post
    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.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  8. #7
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: casting

    Sorry that I am a nitpicker:
    Code:
        char *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.
    Code:
        char searchStr[] = "hi thre there k ";
        char *pr = codecallStrStr(searchStr, "there");
    [edit]And...
    Quote Originally Posted by genux View Post
    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.

  9. #8
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: casting

    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 ?
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  10. #9
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: casting

    Quote Originally Posted by genux View Post
    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.
    Quote Originally Posted by genux View Post
    Is C++ your fav language ?
    Nope.

  11. #10
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: casting

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

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner C# - Casting.
    By CommittedC0der in forum CSharp Tutorials
    Replies: 0
    Last Post: 08-26-2011, 01:13 PM
  2. about casting/downcasting
    By John Jang in forum Java Help
    Replies: 2
    Last Post: 05-16-2011, 04:24 AM
  3. Casting in VB.NET
    By Xav in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 07-29-2008, 11:27 AM
  4. Type Casting
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 06:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts