在 C++ 中取消引用此指针是否有效?
Is it valid to dereference this pointer in C++?
struct myclass{
static const int invalid = -1;
/*explicit*/ myclass(int i, double d = 0.0){
_var = i
}
int _var;
bool operator < (const myclass& rhs);
bool operator > (const myclass& rhs);
bool operator == (const myclass& rhs);
bool operator != (const myclass& rhs);
/*
bool operator == (int rhs){
return *(this) == myclass(rhs); // Is this valid C++ ?
}
bool operator != (int rhs){
return *(this) == myclass(rhs); // Is this valid C++ ?
}
*/
};
int userCodeCall() {
myclass m(10);
// Valid use Case
if(m != myclass::invalid) {
//.... do something
}
// Invalid use case
if(m < 0.5) { // I want this to give me a compiler error
//.... do something
}
// Invalid use case
if(m < 5) { // I want this to give me a compiler error
//.... do something
}
}
我正在处理遗留代码库,我遇到了一个可以从 int 隐式构造的 class。而且我发现了一个错误,我们正在执行 < than 与 double 的比较。我想用构造函数显式解决这个问题,然后用整数单独定义 == 操作。但是,我对设计不满意。因为我在其他任何地方都没有看到这种模式。
bool operator == (int rhs){
return *(this) == myclass(rhs); // Is this valid C++ ?
}
我有两个问题?
C++11/14/17 有效吗?
class 的设计可以改进吗?考虑到我仍然希望使用“==”比较样式来保持有效,因为它遍及用户代码库。
if(m != myclass::invalid) // some similar API need to be supported, if not the same.
您的解决方案很好,但您似乎只是想阻止特定的比较运算符在数字类型位于右侧时起作用。对于 myclass
.
以外的所有类型,您可以通过简单地删除那些特定的运算符来做到这一点
template<typename T>
bool operator<(T) = delete;
template<typename T>
bool operator>(T) = delete;
这种有效性测试更常见的习惯用法是使用显式运算符 bool:
struct myclass {
private:
static const int _invalid = -1;
int _var;
public:
explicit myclass(int i, double d = 0.0){
_var = i
}
explicit operator bool() const { return _var != _invalid; }
bool operator < (const myclass& rhs);
bool operator == (const myclass& rhs);
bool operator != (const myclass& rhs);
bool operator > (const myclass& rhs);
};
int userCodeCall() {
myclass m(10);
// Valid use Case
if(m) {
//.... do something
}
// Invalid use case
if(m < 0.5) { // gives a compile error
//.... do something
}
// Invalid use case
if(m < 5) { // gives a compile error
//.... do something
}
}
如果这太多了,你必须有一个 myclass::invalid 来测试,你可以把它作为 class:
的一个实例
static constexpr myclass invalid(-1);
struct myclass{
static const int invalid = -1;
/*explicit*/ myclass(int i, double d = 0.0){
_var = i
}
int _var;
bool operator < (const myclass& rhs);
bool operator > (const myclass& rhs);
bool operator == (const myclass& rhs);
bool operator != (const myclass& rhs);
/*
bool operator == (int rhs){
return *(this) == myclass(rhs); // Is this valid C++ ?
}
bool operator != (int rhs){
return *(this) == myclass(rhs); // Is this valid C++ ?
}
*/
};
int userCodeCall() {
myclass m(10);
// Valid use Case
if(m != myclass::invalid) {
//.... do something
}
// Invalid use case
if(m < 0.5) { // I want this to give me a compiler error
//.... do something
}
// Invalid use case
if(m < 5) { // I want this to give me a compiler error
//.... do something
}
}
我正在处理遗留代码库,我遇到了一个可以从 int 隐式构造的 class。而且我发现了一个错误,我们正在执行 < than 与 double 的比较。我想用构造函数显式解决这个问题,然后用整数单独定义 == 操作。但是,我对设计不满意。因为我在其他任何地方都没有看到这种模式。
bool operator == (int rhs){
return *(this) == myclass(rhs); // Is this valid C++ ?
}
我有两个问题?
C++11/14/17 有效吗?
class 的设计可以改进吗?考虑到我仍然希望使用“==”比较样式来保持有效,因为它遍及用户代码库。
if(m != myclass::invalid) // some similar API need to be supported, if not the same.
您的解决方案很好,但您似乎只是想阻止特定的比较运算符在数字类型位于右侧时起作用。对于 myclass
.
template<typename T>
bool operator<(T) = delete;
template<typename T>
bool operator>(T) = delete;
这种有效性测试更常见的习惯用法是使用显式运算符 bool:
struct myclass {
private:
static const int _invalid = -1;
int _var;
public:
explicit myclass(int i, double d = 0.0){
_var = i
}
explicit operator bool() const { return _var != _invalid; }
bool operator < (const myclass& rhs);
bool operator == (const myclass& rhs);
bool operator != (const myclass& rhs);
bool operator > (const myclass& rhs);
};
int userCodeCall() {
myclass m(10);
// Valid use Case
if(m) {
//.... do something
}
// Invalid use case
if(m < 0.5) { // gives a compile error
//.... do something
}
// Invalid use case
if(m < 5) { // gives a compile error
//.... do something
}
}
如果这太多了,你必须有一个 myclass::invalid 来测试,你可以把它作为 class:
的一个实例static constexpr myclass invalid(-1);