Topic: Explict and cast/conversion
Author: ajay@unison.com (Ajay Lunawat)
Date: 1995/08/10 Raw View
1 #include <iostream.h>
2
3 class X
4 {
5 int a;
6 public:
7 explicit X(int x):a(x) { cout << "Made an X" << endl; }
8 };
9
10 int main()
11 {
12 X x1(1); // OK
13 X x2 = X(1); // OK
14 X x3 = (X)1; // Error
15 X x4 = (X)(1); // Error
16 X x5 = static_cast<X>(1); // Error
17 }
For the above code gcc2.7.0 gives error for lines 14, 15, 16.
Is it a bug in compiler?
If I remove explicit then every thing work, that is correct.
But are not lines 14 15 and 16 are using explicit conversion/cast like
line 13. Why does it take line 13 OK and not line 17?
Thanks in advance for your help!
Ajay
ajay@unison.com