Welcome Guest ( Log In | Register)



 
Closed TopicStart new topic
> Typecasting In C++
jibnet
post Jun 5 2006, 10:04 AM
Post #1


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 4-June 06
Member No.: 24,718



QUOTE
Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.

For example:
CODE

#include <iostream>

using namespace std;

int main()      
{
  cout<< (char)65 <<"\n";
  // The (char) is a typecast, telling the computer to interpret the 65 as a
  //  character, not as a number.  It is going to give the character output of
  //  the equivalent of the number 65 (It should be the letter A for ASCII).
  cin.get();
}


One use for typecasting for is when you want to use the ASCII characters. For example, what if you want to create your own chart of all 256 ASCII characters. To do this, you will need to use to typecast to allow you to print out the integer as its character equivalent.
CODE

#include <iostream>

using namespace std;

int main()
{
  for ( int x = 0; x < 256; x++ ) {
    cout<< x <<". "<< (char)x <<" ";
    //Note the use of the int version of x to
    // output a number and the use of (char) to
    // typecast the x into a character     
    // which outputs the ASCII character that
    // corresponds to the current number
  }
  cin.get();
}


The typecast described above is a C-style cast, C++ supports two other types. First is the function-style cast:
CODE

int main()      
{
  cout<< char ( 65 ) <<"\n";
  cin.get();
}


This is more like a function call than a cast as the type to be cast to is like the name of the function and the value to be cast is like the argument to the function. Next is the named cast, of which there are four:
CODE

int main()      
{
  cout<< static_cast<char> ( 65 ) <<"\n";
  cin.get();
}

static_cast is similar in function to the other casts described above, but the name makes it easier to spot and less tempting to use since it tends to be ugly. Typecasting should be avoided whenever possible. The other three types of named casts are const_cast, reinterpret_cast, and dynamic_cast. They are of no use to us at this time.


Notice from serverph:

COPIED without proper QUOTES as reported by Avalon.
Sourced from: http://www.cprogramming.com/tutorial/lesson11.html
Quote tags added.
Go to the top of the page
 
+Quote Post
Avalon
post Jun 5 2006, 11:46 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 630
Joined: 12-August 05
From: Melbourne, Australia
Member No.: 10,624



IPB Image

This post has been edited by Avalon: Jun 5 2006, 01:17 PM
Go to the top of the page
 
+Quote Post
serverph
post Jun 5 2006, 05:09 PM
Post #3


Ancient Enigma
Group Icon

Group: [MODERATOR]
Posts: 1,787
Joined: 11-July 04
From: under the stars
Member No.: 76



CLOSED.
Go to the top of the page
 
+Quote Post

Closed TopicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 11th October 2008 - 12:08 PM