468,504 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

Type Conversion and Type Casting

Hi....

Can you please explain me the difference between type conversion and type casting...?
Dec 21 '07 #1
1 4563
weaknessforcats
9,207 Expert Mod 8TB
Type conversion is executing code that you write to orderly convert one type into another.

C++ has two features for this a) conversion constrcutors, b) conversion operators. Conversion constructors convert a type to your class type and conversion operators convert your class type into another type.

A conversion constrcutor is a constructor that has only one argument.

Assume a class Date:
Expand|Select|Wrap|Line Numbers
  1. class Date
  2. {
  3.      public:
  4.         Date(string arg);    //make a Date from a string
  5.         Date(const Date& arg);    //make a Date from another Date
  6.         Date(const char* arg);     //make a Date from a C-style string
  7.         Date(int arg);                  //make a Date from an integer.
  8. };
  9.  
;

In these cases, a Date object can be created and initialized using code that you have written. This is a type conversion.

A typecast is a direction by you to the compiler to change a type in such a way as to violate the rules of C++.

Expand|Select|Wrap|Line Numbers
  1. Date dt("12/21/2007");
  2.  
  3. int* ptr = reinterpret_cast<int*> (&dt);
  4.  
Here dt is a Date and the cast tells the compiler to treat the Date as an integer when you use the ptr pointer. This is nonsense, but it is valid. C++ let's you do stuff like this in case you have some emergency and this is the only way to solve it. Unfortunately, typecasting is often used by programmers who don't understand what they are doing and have to keep forcing the compiler to accept their poor code.

Almosty all typecasting can be avoided if you use a conversion operator. A conversion operator is like your constructor. It is code you write to orderly convert your class into something else.

In the example above, if your Date class looked like this:
Expand|Select|Wrap|Line Numbers
  1. class Date
  2. {
  3.      public:
  4.         Date(string arg);    //make a Date from a string
  5.         Date(const Date& arg);    //make a Date from another Date
  6.         Date(const char* arg);     //make a Date from a C-style string
  7.         Date(int arg);                  //make a Date from an integer.
  8.  
  9.         operator int*();                //make an int* from a Date
  10. };
  11.  
;

The operator int*() is a member function that the compiler can call when a Date needs to be converted into another type. If you have this member function, the compiler will call it to do the reinterpret_cast example.
Dec 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by seia0106 | last post: by
5 posts views Thread by Suzanne Vogel | last post: by
23 posts views Thread by René Nordby | last post: by
10 posts views Thread by lovecreatesbeauty | last post: by
16 posts views Thread by Enekajmer | last post: by
7 posts views Thread by Ben R. | last post: by
9 posts views Thread by Roman Mashak | last post: by
15 posts views Thread by shuisheng | last post: by
25 posts views Thread by SRR | last post: by
8 posts views Thread by Smithers | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.