理解C++编译器为我们做的隐蔽类型转换

导致编译器做出隐蔽类型转换的两种情况:
1.使用单参数构造函数来进行implicit type convertions
什么是单参数构造函数?单参数构造函数是指调用构造函数时只传入一个参数。
下面是单参数构造函数的两个例子:
class Name {                                 // for names of things
public:
  Name(const string& s);                     // converts string to
                                             // Name
  ...
 
};
现在假设我们定义了类Name的一个operator+操作,如下:
inline Name operator+(const Name& name1,const Name& name2)
{
     ……
}
则当我们进行下面的操作时,
Name name,result;
string str;
result = name + str;
因为operator+的参数为Name,而str是string,所以编译器将自动调用构造函数Name(const string& s);将str转换成

Name类型,再进行operator+操作。但是如果在类Name中没有定义像Name(const string& s);这样的构造函数,则语句
result = name + str;将出错。

再看另外一个例子:
class Rational {                             // for rational numbers
public:
  Rational(int numerator = 0,                // converts int to
           int denominator = 1);             // Rational
  ...

};
我们也定义一个函数:
inline Rational operator*(const Rational& r1,const Rational& r2)
{
    ……
}
然后:
Rational r,result;
int i;
result = r + i;
同理,因为i不是Rational类型,必须先进行转换,编译器也将调用构造函数
Rational(int numerator = 0,intdenominator = 1); 编译器为我们做了如下操作:
Rational temp(i);        
result = r + i;
先调用单参数构造函数Rational temp(i);将i转换成Rational类型,再进行operator+操作。
这与上面第一个例子不同在于构造函数的声明:本例中构造函数的声明中有多个参数
Rational(int numerator = 0,int denominator = 1);
而第一个例子构造函数的声明只有一个参数。但本例构造函数声明中为几个参数都提供了默认值。(numerator为默认

值0,denominator为1),因此Rational temp(i);相当于Rational temp(i,1);
从上可以看出:单参数构造函数是指调用时只传入一个参数。(注意:并不是说它必须声明成只带一个参数的函数)
在它的声明中可以是一个参数,也可以是多个参数,但如果是多个参数则必须提供默认值。(其实严格地说,应该是除

了最左边的参数以外,其余的参数必须提供默认值,因为调用单参数构造函数时,参数将传给最左边的参数,而其余的

参数则需要使用默认值,如果在声明时没有设定默认值,将出错)。
A single-argument constructor is a constructor that may be called with only one argument. Such a

constructor may declare a single parameter or it may declare multiple parameters, with each parameter

after the first having a default value

2.使用隐蔽的类型转换操作符implicit type conversion operator来完成类型转换
隐蔽类型转换符是一个operator后面跟一个类型说明符的成员函数。它有这样的形式:operator 类型名();
它可以将它所在的类这种类型转换成operator操作符后所跟的类型名类型。注意:在声明这种函数时,不能够为它的返

回值设定类型,因为它的返回值类型就是函数名所代表的类型。
例如:operator double() const;没有必要写成double operator double() const;
而如果你写成int operator double() const;将出错,因为operator double() const;表示将此成员函数所在的类这种

类型转换成double型。

An implicit type conversion operator is simply a member function with a strange-looking name: the word

operator followed by a type specification. You aren't allowed to specify a type for the function's

return value, because the type of the return value is basically just the name of the function. For

example, to allow Rational objects to be implicitly converted to doubles (which might be useful for

mixed-mode arithmetic involving Rational objects), you might define class Rational like this:
class Rational {
public:
  ...
  operator double() const;                   // converts Rational to
};                                           // double
This function would be automatically invoked in contexts like this:
Rational r(1, 2);                            // r has the value 1/2

double d = 0.5 * r;                          // converts r to a double,
                                             // then does multiplication

最后请注意:在上面两种情况下,编译器能够完成隐蔽类型转换操作的前提是类中定义了合适的构造函数和implicit

type conversion operator。如果没有上面的result = r + i;double d = 0.5 * r; 都会出错。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值