Updated:

less than 1 minute read

개요

  • 암시적 변환 방지


예제

  • 코드
     #include <string>
        
     using namespace std;
        
     class Test1 {
     	private:
     		int i;
        
     	public:
     		Test1(int i){};
     		Test1(string s){};
     		~Test1(){};
     };
        
     class Test2 {
     	private:
     		int i;
        
     	public:
     		explicit Test2(int i){};
     		Test2(string s){};
     		~Test2(){};
     };
        
     void func1(Test1 t) {}
     void func2(Test2 t) {}
        
     int main() {
     	func1(Test1(1));
     	func1(1);
        
     	func2(Test2(2));
     	func2(2);
        
     	return 0;
     }
    
  • 실행 결과
     test.cpp: In function ‘int main()’:
     test.cpp:33:15: error: could not convert ‘2’ from ‘int’ to ‘Test2’
        33 |         func2(2);
           |               ^
           |               |
           |               int