c 2b 2b cast operator

Solutions on MaxInterview for c 2b 2b cast operator by the best coders in the world

showing results for - "c 2b 2b cast operator"
Liv
04 Jul 2017
1struct X {
2    //implicit conversion
3    operator int() const { return 7; }
4 
5    // explicit conversion
6    explicit operator int*() const { return nullptr; }
7 
8//   Error: array operator not allowed in conversion-type-id
9//   operator int(*)[3]() const { return nullptr; }
10    using arr_t = int[3];
11    operator arr_t*() const { return nullptr; } // OK if done through typedef
12//  operator arr_t () const; // Error: conversion to array not allowed in any case
13};
14 
15int main()
16{
17    X x;
18 
19    int n = static_cast<int>(x);   // OK: sets n to 7
20    int m = x;                     // OK: sets m to 7
21 
22    int* p = static_cast<int*>(x);  // OK: sets p to null
23//  int* q = x; // Error: no implicit conversion
24 
25    int (*pa)[3] = x;  // OK
26}