473,513 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type Conversion and Type Casting

3 New Member
Hi....

Can you please explain me the difference between type conversion and type casting...?
Dec 21 '07 #1
1 4907
weaknessforcats
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

2
6033
by: seia0106 | last post by:
Hello I am writing a function to read a binary file. Here is a part of code #include <fstream> .. .. BYTE *pData; long lDataLen; pms->GetPointer(&pData); lDataLen = pms->GetSize(); // Read...
5
8240
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
23
3477
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
10
2242
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
16
12772
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
7
4211
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
9
430
by: Roman Mashak | last post by:
Hello, All! I met this code recently on some open source sites. What may be the point of using such construction: typedef struct cmd { unsigned int cmdack; unsigned int code; unsigned int...
15
7554
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
25
3469
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
8
3132
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.