Last active
June 19, 2020 08:16
-
-
Save CroMarmot/d3896db2512c8114f634fcf38d292d39 to your computer and use it in GitHub Desktop.
Disable Implicit Casts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class CStudent | |
{ | |
public: | |
CStudent() {}; | |
~CStudent() {}; | |
}; | |
class CTeacher | |
{ | |
public: | |
CTeacher() {}; | |
CTeacher(CStudent student) | |
: m_student(student) | |
{ | |
} | |
~CTeacher() {}; | |
void Teach() {cout<<"teacher"<<endl;} | |
private: | |
CStudent m_student; | |
}; | |
// http://www.gockelhut.com/cpp-pirate/disable-implicit-casts.html | |
// 禁止函数调用 参数的隐式转换 | |
template <typename T> | |
void foo(T) = delete; | |
void foo(CTeacher teacher) | |
{ | |
teacher.Teach(); | |
} | |
int main() | |
{ | |
CStudent student; | |
CTeacher student_teacher; | |
foo(student); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment