SlideShare una empresa de Scribd logo
1 de 12
S202 4- 1
Implicit conversions occur when mixed type
expressions are evaluated or when the actual
arguments in a function call do not match the formal
arguments of the function prototype.
First, the compiler tries to apply a trivial conversion.
If there is no match, then the compiler attempts to
apply a promotion.
If there is no match, then the compiler attempts to
apply a built-in type conversion.
If there is no match, then the compiler attempts to
apply a user defined type conversion.
If there is no match, the compiler generates an error.
Implicit Conversions
S202 4- 2
To determine if a user defined type conversion is used,
the compiler checks if a conversion is defined for the
type needed.
If there is, then the compiler checks that the type
matches the type supplied or that the type supplied can
be converted by applying one of the built-in type
promotions or conversions.
Only one such built-in type promotion or conversion
will be applied before applying the user defined type
conversion itself.
Thus, at most one built-in type conversion and at most
one user defined type conversion will ever be implicitly
applied when converting from one type to another.
Implicit Conversions
S202 4- 3
Explicit conversions occur when the client explicitly
invokes a cast operation.
Both implicit and explicit type conversions result in a
temporary object of the type being converted to.
This temporary object is used in place of the original.
The value of the original object is not affected.
When considering execution efficiency, it is important
to reduce or minimize the creation of such temporaries.
We recommend minimizing user defined conversions
as much as possible by avoiding mixed type
expressions and by supplying arguments to functions
that exactly match the type required by the function
prototypes.
Explicit Conversions
S202 4- 4
Constructors taking a single argument define a
conversion from the type of its argument to the type of
its class.
Such conversion functions can be used either implicitly
or explicitly.
When used implicitly, at most one implicit built-in
promotion or conversion will be applied to the
argument of the constructor. name(char* = "");
//implicitly convert char* to name
name obj; obj = "sue smith";
User Defined Type Convers.
S202 4- 5
Remember, the lifetime of a temporary object is from
the time it is created until the end of the statement in
which it was created.
In the assignment statement, the temporary object is
destroyed after the temporary is assigned.
As a formal argument (passed by value), the temporary
object is destroyed after the function is called.
Therefore, not only are temporary objects formed (and
the constructor implicitly invoked) but the objects are
also destroyed (and the destructors are implicitly
invoked).
User Defined Type Convers.
S202 4- 6
If we do not want a constructor taking a single
argument to also define an implicit conversion
function, we can prevent that by preceding the
constructor declaration with the keyword explicit.
The application is now required to provide explicit type
casts in order to convert a char* to a name object.
Using a constructor as a conversion function allows us
to convert an object of some other type to an object of a
class. They do not allow us to define a conversion from
a class to some other built-in type or class.
To do so, we must define a type conversion function.
User Defined Type Convers.
S202 4- 7
A conversion function allows us to define a conversion
from an object of a class to another built-in type.
Conversion functions are also useful when we need to
convert from an object of our class to an object of a class
that we do not have access to or do not want to modify.
When we do have access to the class and are willing to
modify it, we can always define a constructor taking a
single argument of our class type to perform the
conversion.
operator other_type();
User Defined Type Convers.
S202 4- 8
Notice that this conversion function has no return type.
That is because the return type is implicitly specified
(other_type), since that is the type we are converting to.
The conversion function converts the current object into
the new type and returns the value.
The conversion function can be called either implicitly
or explicitly.
The name of the conversion function must be a single
identifier; therefore, for types that are not a single
identifier, a typedef must first be used to create a single
identifier.
User Defined Type Convers.
S202 4- 9
Notice that this conversion function has no return type.
That is because the return type is implicitly specified
(other_type), since that is the type we are converting to.
The conversion function converts the current object into
the new type and returns the value.
The name of the conversion function must be a single
identifier; therefore, for types that are not a single
identifier, a typedef must first be used. In the class:
typedef const char* pchar; //make single identifier
operator pchar(); //conversion (name to char*)
Implementing:
name::operator pchar() { //conversion function
...
return array; //of type char * being returned
}
User Defined Type Convers.
S202 4- 10
In order for the conversion function to be called
explicitly when it is a typedef name, that name must be
in the global namespace and cannot be hidden within
the class.
name obj("sue smith");
const char* p;
//Three examples of explicit conversions:
p = (pchar)obj;
p = pchar(obj);
p = static_cast<pchar>(obj);
User Defined Type Convers.
S202 4- 11
With constructors as conversion functions and also
conversion functions, avoid mutual conversions.
Many times type conversions are specified to minimize
the number of operators overloaded, especially for
symmetric operators where there can be multiple
combinations of types for the two operands. It can
reduce the number of versions of an operator to a
single, non-member.
However, this can produce subtle changes in the way a
program works. If we were to add a string to char *
user defined conversion function to our string class, all
uses of operator+ containing both char * and string
types would become ambiguous.
User Defined Type Convers.
S202 4- 12
Type conversions will not help when operators are
members -- because a member is required to have the
first operand as an object of the class; if it converted to
some other type, the member function will not be
found as a possible candidate function and a compile
error will result or the wrong operator will be used
(such as the built-in operators).
Also, when user defined conversions to built-in types
have been specified, the predefined operators may end
up being used even when we use an operator with a
user defined type for which an overloaded operator
exists! Overloaded operators are only invoked if at least
one of those operands is a user defined type.
User Defined Type Convers.

Más contenido relacionado

La actualidad más candente (20)

Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Strings
StringsStrings
Strings
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Enums in c
Enums in cEnums in c
Enums in c
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 

Destacado

Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Type checking
Type checkingType checking
Type checkingrawan_z
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversionsAmogh Kalyanshetti
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cppgourav kottawar
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
BCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesBCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesstudent
 
Efficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based modelsEfficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based modelsFrancisco Zamora-Martinez
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityAakash Singh
 

Destacado (20)

Type Casting in C++
Type Casting in C++Type Casting in C++
Type Casting in C++
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Type checking
Type checkingType checking
Type checking
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Run time storage
Run time storageRun time storage
Run time storage
 
BCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesBCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codes
 
Efficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based modelsEfficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based models
 
Compiler1
Compiler1Compiler1
Compiler1
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Jscript part1
Jscript part1Jscript part1
Jscript part1
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 

Similar a Type conversions

C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/Geekyants
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in PythonMars Devs
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANINikul4470
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12Dev Chauhan
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwMayankSinghRawat6
 
C questions
C questionsC questions
C questionsparm112
 

Similar a Type conversions (20)

C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Function in C++
Function in C++Function in C++
Function in C++
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12
 
C functions
C functionsC functions
C functions
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
 
C questions
C questionsC questions
C questions
 

Más de sanya6900

.Net framework
.Net framework.Net framework
.Net frameworksanya6900
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IISsanya6900
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IISsanya6900
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handlingsanya6900
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++sanya6900
 
Memory allocation
Memory allocationMemory allocation
Memory allocationsanya6900
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03sanya6900
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)sanya6900
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2sanya6900
 

Más de sanya6900 (13)

.Net framework
.Net framework.Net framework
.Net framework
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IIS
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IIS
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Ch7
Ch7Ch7
Ch7
 
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Pointers
PointersPointers
Pointers
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
 

Último

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 

Último (20)

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 

Type conversions

  • 1. S202 4- 1 Implicit conversions occur when mixed type expressions are evaluated or when the actual arguments in a function call do not match the formal arguments of the function prototype. First, the compiler tries to apply a trivial conversion. If there is no match, then the compiler attempts to apply a promotion. If there is no match, then the compiler attempts to apply a built-in type conversion. If there is no match, then the compiler attempts to apply a user defined type conversion. If there is no match, the compiler generates an error. Implicit Conversions
  • 2. S202 4- 2 To determine if a user defined type conversion is used, the compiler checks if a conversion is defined for the type needed. If there is, then the compiler checks that the type matches the type supplied or that the type supplied can be converted by applying one of the built-in type promotions or conversions. Only one such built-in type promotion or conversion will be applied before applying the user defined type conversion itself. Thus, at most one built-in type conversion and at most one user defined type conversion will ever be implicitly applied when converting from one type to another. Implicit Conversions
  • 3. S202 4- 3 Explicit conversions occur when the client explicitly invokes a cast operation. Both implicit and explicit type conversions result in a temporary object of the type being converted to. This temporary object is used in place of the original. The value of the original object is not affected. When considering execution efficiency, it is important to reduce or minimize the creation of such temporaries. We recommend minimizing user defined conversions as much as possible by avoiding mixed type expressions and by supplying arguments to functions that exactly match the type required by the function prototypes. Explicit Conversions
  • 4. S202 4- 4 Constructors taking a single argument define a conversion from the type of its argument to the type of its class. Such conversion functions can be used either implicitly or explicitly. When used implicitly, at most one implicit built-in promotion or conversion will be applied to the argument of the constructor. name(char* = ""); //implicitly convert char* to name name obj; obj = "sue smith"; User Defined Type Convers.
  • 5. S202 4- 5 Remember, the lifetime of a temporary object is from the time it is created until the end of the statement in which it was created. In the assignment statement, the temporary object is destroyed after the temporary is assigned. As a formal argument (passed by value), the temporary object is destroyed after the function is called. Therefore, not only are temporary objects formed (and the constructor implicitly invoked) but the objects are also destroyed (and the destructors are implicitly invoked). User Defined Type Convers.
  • 6. S202 4- 6 If we do not want a constructor taking a single argument to also define an implicit conversion function, we can prevent that by preceding the constructor declaration with the keyword explicit. The application is now required to provide explicit type casts in order to convert a char* to a name object. Using a constructor as a conversion function allows us to convert an object of some other type to an object of a class. They do not allow us to define a conversion from a class to some other built-in type or class. To do so, we must define a type conversion function. User Defined Type Convers.
  • 7. S202 4- 7 A conversion function allows us to define a conversion from an object of a class to another built-in type. Conversion functions are also useful when we need to convert from an object of our class to an object of a class that we do not have access to or do not want to modify. When we do have access to the class and are willing to modify it, we can always define a constructor taking a single argument of our class type to perform the conversion. operator other_type(); User Defined Type Convers.
  • 8. S202 4- 8 Notice that this conversion function has no return type. That is because the return type is implicitly specified (other_type), since that is the type we are converting to. The conversion function converts the current object into the new type and returns the value. The conversion function can be called either implicitly or explicitly. The name of the conversion function must be a single identifier; therefore, for types that are not a single identifier, a typedef must first be used to create a single identifier. User Defined Type Convers.
  • 9. S202 4- 9 Notice that this conversion function has no return type. That is because the return type is implicitly specified (other_type), since that is the type we are converting to. The conversion function converts the current object into the new type and returns the value. The name of the conversion function must be a single identifier; therefore, for types that are not a single identifier, a typedef must first be used. In the class: typedef const char* pchar; //make single identifier operator pchar(); //conversion (name to char*) Implementing: name::operator pchar() { //conversion function ... return array; //of type char * being returned } User Defined Type Convers.
  • 10. S202 4- 10 In order for the conversion function to be called explicitly when it is a typedef name, that name must be in the global namespace and cannot be hidden within the class. name obj("sue smith"); const char* p; //Three examples of explicit conversions: p = (pchar)obj; p = pchar(obj); p = static_cast<pchar>(obj); User Defined Type Convers.
  • 11. S202 4- 11 With constructors as conversion functions and also conversion functions, avoid mutual conversions. Many times type conversions are specified to minimize the number of operators overloaded, especially for symmetric operators where there can be multiple combinations of types for the two operands. It can reduce the number of versions of an operator to a single, non-member. However, this can produce subtle changes in the way a program works. If we were to add a string to char * user defined conversion function to our string class, all uses of operator+ containing both char * and string types would become ambiguous. User Defined Type Convers.
  • 12. S202 4- 12 Type conversions will not help when operators are members -- because a member is required to have the first operand as an object of the class; if it converted to some other type, the member function will not be found as a possible candidate function and a compile error will result or the wrong operator will be used (such as the built-in operators). Also, when user defined conversions to built-in types have been specified, the predefined operators may end up being used even when we use an operator with a user defined type for which an overloaded operator exists! Overloaded operators are only invoked if at least one of those operands is a user defined type. User Defined Type Convers.