User-defined conversions

 今天才发现operator的关于转换函数的用法,真是土鳖了~~~

从IBM上转篇文章来记录一下

 

User-defined conversions allow you to specify object conversions with constructors or with conversion functions. User-defined conversions are implicitly used in addition to standard conversions for conversion of initializers, functions arguments, function return values, expression operands, expressions controlling iteration, selection statements, and explicit type conversions.

There are two types of user-defined conversions:

  • Conversion constructors
  • Conversion functions

The compiler can use only one user-defined conversion (either a conversion constructor or a conversion function) when implicitly converting a single value. The following example demonstrates this:

  1. class A {
  2.   int x;
  3. public:
  4.   operator int() { return x; };
  5. };
  6. class B {
  7.   A y;
  8. public:
  9.   operator A() { return y; };
  10. };
  11. int main () {
  12.   B b_obj;
  13. //  int i = b_obj;
  14.   int j = A(b_obj);
  15. }

The compiler would not allow the statement int i = b_obj. The compiler would have to implicitly convert b_obj into an object of type A (with B::operator A()), then implicitly convert that object to an integer (with A::operator int()). The statement int j = A(b_obj) explicitly converts b_obj into an object of type A, then implicitly converts that object to an integer.

User-defined conversions must be unambiguous, or they are not called. A conversion function in a derived class does not hide another conversion function in a base class unless both conversion functions convert to the same type. Function overload resolution selects the most appropriate conversion function. The following example demonstrates this:

  1. class A {
  2.   int a_int;
  3.   char* a_carp;
  4. public:
  5.   operator int() { return a_int; }
  6.   operator char*() { return a_carp; }
  7. };
  8. class B : public A {
  9.   float b_float;
  10.   char* b_carp;
  11. public:
  12.   operator float() { return b_float; }
  13.   operator char*() { return b_carp; }
  14. };
  15. int main () {
  16.   B b_obj;
  17. //  long a = b_obj;
  18.   char* c_p = b_obj;
  19. }

The compiler would not allow the statement long a = b_obj. The compiler could either use A::operator int() or B::operator float() to convert b_obj into a long. The statement char* c_p = b_obj uses B::operator char*() to convert b_obj into a char* because B::operator char*() hides A::operator char*().

When you call a constructor with an argument and you have not defined a constructor accepting that argument type, only standard conversions are used to convert the argument to another argument type acceptable to a constructor for that class. No other constructors or conversions functions are called to convert the argument to a type acceptable to a constructor defined for that class. The following example demonstrates this:

 

  1. class A {
  2. public:
  3.   A() { }
  4.   A(int) { }
  5. };
  6. int main() {
  7.    A a1 = 1.234;
  8. //   A moocow = "text string";
  9. }

The compiler allows the statement A a1 = 1.234. The compiler uses the standard conversion of converting 1.234 into an int, then implicitly calls the converting constructor A(int). The compiler would not allow the statement A moocow = "text string"; converting a text string to an integer is not a standard conversion.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值