OOPortal
RationalDB Database Design
prev next prev next
  Course navigation
 
Lesson 6
Objective
ADT conversions
Comparing conversion operators and constructors
    Understand when to use conversion operators and when to use constructors to perform conversion.
Here is a table that explains the differences between using a constructor for conversions and using a special conversion function.
Conversion Constructor Conversion Operator
used to convert from built-in types to user-defined types used to convert from user-defined to built-in types
always has only one "from-type" argument, while the "to-type" argument is implied has no parameters
creates new objects of user-defined type has no return type; creates built-in type object
available for both implicit and explicit conversions (it is used explicitly in either cast or functional form) conversions occur implicitly in assignment expressions, in arguments to functions, and in values returned from functions
Please note: A conversion member function of the form A::operator B() and a constructor of the form B::B(const A&) both provide conversions from type A objects to type B objects. Having both in your class can result in ambiguity errors.
ADT Conversions Constructor - Quiz
Click the Quiz link below to take a short multiple-choice quiz on ADT conversions.
ADT Conversions Constructor - Quiz
  Course navigation