Accidentally Using a Single-Argument Constructor as a Conversion Constructor

The program (Fig. 10.12) uses the Array class of Figs. 10.1010.11 to demonstrate an improper implicit conversion. To allow this implicit conversion, we removed the explicit keyword from line 14 in Array.h (Fig. 10.10).

 1   // Fig. 10.12: fig10_12.cpp 2   // Single-argument constructors and implicit conversions. 3   #include <iostream> 4   #include "Array.h"  5   using namespace std; 6  7   void outputArray( const Array & ); // prototype 8  9   int main()10   {11      Array integers1( 7 ); // 7-element Array12      outputArray( integers1 ); // output Array integers113      outputArray( 3 ); // convert 3 to an Array and output Array's contents ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.