Advertisement
mbazs

C++ explicit ctor

Jun 22nd, 2019
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class A {
  4. public:
  5.     explicit A(int n): n(n) {
  6.         std::cout << "A(int) ctor: " << n << std::endl;
  7.     }
  8. private:
  9.     int n;
  10. };
  11.  
  12. class B {
  13. public:
  14.     B(int n): n(n) {}
  15.     operator int() {return n;} // implicit conversion operator
  16. private:
  17.     int n;
  18. };
  19.  
  20. int main()
  21. {
  22.     A x(5);      // OK
  23.     A y(B(15));  // why compiles?
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement