c – 如果参数条件为真,则阻止执行返回函数
发布时间:2020-12-16 04:58:19 所属栏目:百科 来源:网络整理
导读:就像在标题上说的那样,我想知道是否有可能阻止执行功能.在我的特定情况下,我正在尝试创建一个operator []并阻止utilisator使用它,如果参数中的值太高: 在.h: class Vec4{ float x,y,z,w; public: float operator[](const unsigned int i);} 在.cpp中: flo
就像在标题上说的那样,我想知道是否有可能阻止执行功能.在我的特定情况下,我正在尝试创建一个operator []并阻止utilisator使用它,如果参数中的值太高:
在.h: class Vec4 { float x,y,z,w; public: float operator[](const unsigned int i); } 在.cpp中: float Vec4::operator[](const unsigned int i) { if(i == 0) return x; if(i == 1) return y; if(i == 2) return z; if(i == 3) return w; } 如果我> = 4,我想“破坏”这个功能 谢谢你告诉我,如果有办法……或不! 解决方法
你至少可以做4件事.
>从函数中返回已知的错误值.例如. -1如果输入值不好. 选项1 float Vec4::operator[](const unsigned int i) { switch (i) case 0: return x; ... default: return nan; 选项2 default: throw InvalidInputException; 选项3 typedef ErrCode int; const int ERROR = -1; const int SUCCESS = 1; ... ErrCode Vec4::getPoint(const unsigned int i,float &ouptut) { ... switch (i) case 0: output = x; return SUCCESS; default: return ERROR; 备选案文4(c 11) class Vec4 { ... public: enum class VecMem {X,Y,Z,W}; float Vec4::getPoint(VecMem member) { switch (member): case X: return x; ... 用法: Vec4.getPoint(Vec4::VecMem::X); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |