OOPortal OOPortal


Ad Hoc Polymorphism  «Prev 

Conversion constructors in C++ - Exercise Results

You entered:

Here is one solution to this exercise:
1. The two constructors
ch_stack(const char* c);
explicit ch_stack(int size);

are conversion constructors.
The use of the keyword explicit in front of ch_stack(int size) suppresses undesirable implicit conversion.
2. The lines
ch_stack a = 'a';   //1
...
ch_stack d = 10; //4

are error-prone because ch_stack(int size) is declared as an explicit constructor and there is no implicit conversion available.