Lecture overview -- Keyboard shortcut: 'u'  Previous page: Preventing object copying -- Keyboard shortcut: 'p'  Next page: Implicit Conversion -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 3 - Page 18 : 36
Notes about C++
Abstraction Mechanisms, Part 1
Classes and Conversion

Given a user defined type T, programmed via a class or struct:

It is possible to convert from some type S to type T via constructors in T

It is possible to convert from type T to some type S via conversion operators in T

Item 5 of More Effective C++ is informative about implicit type conversions

 

  • In which cases are conversion operators necessary? (Why not use a constructor in S of parameter T)

    • When converting to built in types

    • We cannot program constructors (with a T parameter) in a built-in type S

  • Conversion operator syntax - such as conversion to type int from class T

    • T::operator int () const {...}

    • Not:     int T::operator int () const {...}

    • Thus, constructor-like syntax

  while (cin >> i)   // convert istream to bool or int
     cout << i*2 << " " << endl;

istream conversion to bool.

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/conversions/io-stream-1.ccThe while loop in the context of a full C++ program.