How to disable implicit conversion in C++

1 Answer

0 votes
#include <iostream>
 
class Example {
private:
    int m_A;
public:
     
    explicit Example(int a) : m_A(a) {} // explicit disable implicit Conversion
     
    void Print() const {
        std::cout << m_A << "\n";
    }
};
   
  
int main() {
    Example ex = 98; // Implicit Conversion // Constrct Example with 98
       
    ex.Print();
}
    
    
    
    
/*
run:
    
error: conversion from ‘int’ to non-scalar type ‘Example’ requested
    
*/

 



answered Feb 2, 2023 by avibootz

Related questions

2 answers 185 views
1 answer 105 views
105 views asked Jun 25, 2022 by avibootz
1 answer 126 views
126 views asked Dec 4, 2022 by avibootz
1 answer 183 views
183 views asked May 11, 2018 by avibootz
1 answer 159 views
159 views asked May 11, 2018 by avibootz
1 answer 200 views
200 views asked Jul 6, 2018 by avibootz
...