C++ 中的用户定义转换
最近,我正在浏览 O'Reilly Media 的《C++ Pocket Reference》副本,当我看到有关用户定义类型的用户定义转换的简短部分和示例时,我感到很惊讶:
#include <iostream>
class account {
private:
double balance;
public:
account (double b) { balance = b; }
operator double (void) { return balance; }
};
int main (void) {
account acc(100.0);
double balance = acc;
std::cout << balance << std::endl;
return 0;
}
我已经用 C++ 编程有一段时间了,这是我第一次看到这种运算符重载。这本书对这个主题的描述有些简短,给我留下了一些关于这个功能未解答的问题:
- 这是一个特别晦涩的功能吗?正如我所说,我用 C++ 编程已经有一段时间了,这是我第一次遇到这种情况。我没有太多运气找到与此相关的更深入的材料。
- 这个相对便携吗? (我正在 GCC 4.1 上进行编译)
可以完成用户定义类型到用户定义类型的转换吗?例如
operator std::string () { /* code */ }
Recently, I was browsing through my copy of the C++ Pocket Reference from O'Reilly Media, and I was surprised when I came across a brief section and example regarding user-defined conversion for user-defined types:
#include <iostream>
class account {
private:
double balance;
public:
account (double b) { balance = b; }
operator double (void) { return balance; }
};
int main (void) {
account acc(100.0);
double balance = acc;
std::cout << balance << std::endl;
return 0;
}
I've been programming in C++ for awhile, and this is the first time I've ever seen this sort of operator overloading. The book's description of this subject is somewhat brief, leaving me with a few unanswered questions about this feature:
- Is this a particularly obscure feature? As I said, I've been programming in C++ for awhile and this is the first time I've ever come across this. I haven't had much luck finding more in-depth material regarding this.
- Is this relatively portable? (I'm compiling on GCC 4.1)
Can user-defined conversions to user defined types be done? e.g.
operator std::string () { /* code */ }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,转换运算符并不经常使用。我见过它们的地方是用户定义的类型,可以降级为内置类型。像支持与原子数类型之间转换的固定精度数字类之类的东西。
据我所知,确实如此。它们永远处于标准之中。
是的,这是构造函数的功能之一。采用单个参数的构造函数有效地创建从参数类型到类类型的转换运算符。例如,像这样的类:
让你这样做:
如果您以前使用过
std::string
,那么您很可能在没有意识到的情况下使用过此功能。 (顺便说一句,如果您想防止这种行为,请使用显式
声明任何单参数构造函数。)Yes, conversion operators aren't used very often. The places I've seen them are for user-defined types that can degrade to built-in ones. Things like a fixed-precision number class that supports converting to/from atomic number types.
As far as I know, it is. They've been in the standard forever.
Yes, that's one of the features of constructors. A constructor that takes a single argument effectively creates a conversion operator from the argument type to your class's type. For example, a class like this:
Let's you do:
If you've used
std::string
before, odds are you've used this feature without realizing it. (As an aside, if you want to prevent this behavior, declare any single-argument constructors usingexplicit
.)它并不是特别晦涩难懂;它非常可移植(毕竟它是语言的一部分),并且可以转换为用户定义的类型。
需要注意的是,拥有许多可能的隐式转换路径可能会导致意外的转换被调用和令人惊讶的错误。此外,在多个用户定义类型之间使用非显式转换构造函数和转换函数可能会导致更加模糊的转换序列,这可能很难解决。
It's not particularly obscure; it is very portable (it is part of the language after all), and conversion to user-defined types is possible.
One word of caution, having a lot of possible implicit conversion paths can lead to unexpected conversion being invoked and surprising bugs. Also, having non-explicit converting constructors and conversion functions between several user-defined types can lead to more ambigious conversion sequeunces which can be a pain to resolve.
这是我学习 C++ 时偶然发现的第一件事,所以我想说,不,它并不那么晦涩难懂。
不过,我要警告一件事:除非您确切知道自己在做什么,否则请对它们使用
explicit
关键字。隐式转换可能会导致代码出现意外行为,因此在大多数情况下应避免使用它们。坦率地说,如果该语言没有它们,我会更高兴。It was about one of the first things I stumbled across when I was learning C++, so I'd say that no, it is not all that obscure.
One thing I would caution on though: Use the
explicit
keyword with them unless you know exactly what you are doing. Implicit conversions can cause code to behave in unexpected ways, so you should avoid using them in most cases. Frankly, I'd be happier if the language didn't have them.这是一个特别有用的标准 C++ 功能,而且一点也不晦涩:)
您可以将基本类型和用户定义类型类似地用于转换运算符。
This is a particulary useful standard C++ feature and not a bit obscure :)
You can use fundamental and user defined types alike for conversion operators.