是否有c ++的(至少更多或更少的)标准int类?
如果没有,C++13计划如何,并且如果不是这样,是否存在任何特殊原因?
OOP设计可能会从中受益,例如,在自定义类中具有返回int的赋值运算符将是很好的:
int i=myclass;
而不是
int i=myclass.getInt();
好的,有很多例子可以展示它的实用性,如果还没有的话,为什么不开发出来呢?
这是为了进行死推算和其他滞后补偿方案而设计的,将这些值视为“正常”的变量将会很好,希望如此!
在自定义类中添加一个返回int的赋值运算符会很有用。
您可以使用转换运算符来实现:
class myclass {
int i;
public:
myclass() : i(42) {}
// Allows implicit conversion to "int".
operator int() {return i;}
};
myclass m;
int i = m;
通常情况下应该避免这样做,因为额外的隐式转换可能会引入歧义,或隐藏类型系统本应捕捉到的类别错误。在C++11中,您可以通过声明操作符explicit
来防止隐式转换;然后可以使用该类来初始化目标类型,但不会被隐式转换:
int i(m); // OK, explicit conversion
i = m; // Error, implicit conversion
int
的包装类,可以通过JNI通知Java对象其值的任何更改。通过使用转换运算符,我能够重载赋值/修改运算符(=
,++
,--
)以通知Java对象,而不破坏现有引用对象作为整数的功能。由于无法扩展int
,因为它是一种基本数据类型而不是类,因此这似乎是一个很好的解决方案。我是否遗漏了什么,或者这是我的问题的好解决方案? - StockB如果你想让你的类隐式转换为 int
,你可以使用一个隐式转换运算符 (operator int()
),但一般来说,隐式转换会引起更多的问题和调试,而不是解决使用上的便利。
int
,那么其他答案中提出的转换运算符解决方案就可以了。但是,你的myclass
模型是什么呢?从中获取一个整数意味着什么?这是你需要考虑的问题,然后你应该得出结论:在没有任何信息的情况下获得整数很可能没有意义。
以std::vector<T>::size()
为例,它返回一个整数。难道std::vector<T>
应该因此可转换为整数吗?我不这样认为。方法应该被称为getInt()
吗?同样,我也不这样认为。从一个名字中,你只知道它返回一个整数,无法了解它返回的具体内容。此外,它并不是唯一返回整数的方法,还有capacity()
。为你的类实现operator int ()运算符
这可以通过使用强制类型转换运算符来实现。例如:
class MyClass {
private:
int someint;
public:
operator const int() {
return this->someint;
}
}
不会有的,也不需要。你想要做的可以通过转换运算符来实现:
#include <iostream>
struct foo {
int x;
foo(int x) : x(x) {}
operator int() { return x; }
};
int main() {
foo x(42);
int y(x);
std::cout << y;
}
Byte
到 int
的隐式转换(在我看来是适当的),还是从 int
到 Byte
的转换(在我看来是不合适的)导致了问题? - celtschkint i=myclass;
struct MyClass {
operator int() {
return v;
}
int v;
} myclass = {2};
int i = myclass; // i = 2
并非所有东西都必须是“面向对象”的。C++提供了其他选择。
没有标准的 int 类。对于 BigDecimal 等类,您可以查看 是否有 C++ 版本的 Java 的 BigDecimal?
至于 int,如果您真的需要它,可以自己创建一个。我从未遇到过需要 Integer 类的情况。
有明显的理由为int创建一个类,因为int本身不允许没有任何值的存在。例如,考虑一个JSON消息。它可以包含一个名为“foo”的对象的定义,以及一个名为“bar”的整数:
{"foo": {"bar": 0}}
{"foo": {}}
int main(int argc, char **argv) {
xString name;
xInt age;
std::cout<< "before assignment:" << std::endl;
std::cout<< "name is " << name << std::endl;
std::cout<< "age is " << age << std::endl;
// some data collection/transfer occurs
age = 32;
name = "john";
// data validation
if (name.isNull()) {
throw std::runtime_error("name was not supplied");
}
if (age.isNull()) {
throw std::runtime_error("age was not supplied");
}
// data output
std::cout<< std::endl;
std::cout<< "after assignment:" << std::endl;
std::cout<< "name is " << name << std::endl;
std::cout<< "age is " << age << std::endl;
return 0;
}
before assignment:
name is null
age is null
after assignment:
name is john
age is 32
#include <iostream>
#include <string>
class xInt {
private:
int _value=0;
bool _isNull=true;
public:
xInt(){}
xInt(int value) {
_value=value;
_isNull=false;
}
bool isNull(){return _isNull;}
int value() {return _value;}
void unset() {
_value=0;
_isNull=true;
}
friend std::ostream& operator<<(std::ostream& os, const xInt& i) {
if (i._isNull) {
os << "null";
} else {
os << i._value;
}
return os;
}
xInt& operator=(int value) {
_value=value;
_isNull=false;
return *this;
}
operator const int() {
return _value;
}
};
class xString {
private:
std::string _value;
bool _isNull=true;
public:
xString(){}
xString(int value) {
_value=value;
_isNull=false;
}
bool isNull() {return _isNull;}
std::string value() {return _value;}
void unset() {
_value.clear();
_isNull=true;
}
friend std::ostream& operator<<(std::ostream& os, const xString& str) {
if (str._isNull) {
os << "null";
} else {
os << str._value;
}
return os;
}
xString& operator<<(std::ostream& os) {
os << _value;
return *this;
}
xString& operator=(std::string value) {
_value.assign(value);
_isNull=false;
return *this;
}
operator const std::string() {
return _value;
}
};
没有理由需要一个,因此不会有任何的。
int
是一个最终类,重载运算符并内置支持文字。因此,5 将创建类 int
的对象。为什么要这样做?嗯,如果 C++ 还添加了一些方法,例如“repeat”,则您可以编写类似于“5.repeat([]{your_code;});”的内容。难道只有一些向后兼容性问题,例如 type_traits 等吗? - sasha.sochkarepeat(5, []{ your code; });
。即使是C++委员会也不够疯狂,将int
变成一个类。C++类型系统已经由对象(除了引用)组成。没有理由改变现有的基本类型,它们已经是对象了。 - Puppy
int
有什么问题? - Konrad Rudolphint
无法提供? - tenfour