472,950 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

Type Conversion and Type Casting

Hi....

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

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

Similar topics

2
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
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
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.