Skip to content

Instantly share code, notes, and snippets.

@CroMarmot
Last active June 19, 2020 08:16
Show Gist options
  • Save CroMarmot/d3896db2512c8114f634fcf38d292d39 to your computer and use it in GitHub Desktop.
Save CroMarmot/d3896db2512c8114f634fcf38d292d39 to your computer and use it in GitHub Desktop.
Disable Implicit Casts
#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