【问题标题】:implicit conversion隐式转换
【发布时间】:2011-03-08 05:06:45
【问题描述】:

在下面的例子中,作者对隐式转换做了几个cmet。你能解释一下这些cmets的更多细节吗,我不是很清楚。谢谢。

class String{
  explicit String(int n);
  String(const char *p);
}
String  s1= ‘a’;     //error:  no implicit char->String conversion
void f(String);
String g( )
{
   f(10);           // error: no implicit int->String conversion
   return 10;   //  error:  no implicit int-> String conversion
}

【问题讨论】:

  • 您到底有什么不清楚的地方?这三个中的每一个都是需要隐式转换的示例;由于将使用的构造函数是explicit,因此无法进行隐式转换。

标签: c++ operator-overloading


【解决方案1】:

String 类有两个构造函数;一种用于从int 构造String,另一种用于从指向char 的常量指针构造String。因此,这两个构造函数也是转换函数,因为它们确实将一种类型转换为另一种类型。然而,第一个构造函数是 explicit 构造函数。虽然第二个构造函数允许从指向char 的指针隐式转换为String,但第一个构造函数要求您显式请求转换。

例如:

String s;
s = 10;          // error: implicit conversion from int to String
s = String(10);  // ok: explicit conversion of int to String.

第一个错误注释只是说没有将char 转换为String 的构造函数。同样,我们只有两个构造函数:一个用于转换 int,另一个是指向 char 的 const 指针。

第二个错误涉及将int 作为参数传递给需要String 的函数。这意味着该函数必须从int隐式构造一个String。这无法完成,因为相关的构造函数是显式的。如果您从int 构造String,然后将String 传递给函数,一切都会好起来的。

第三个错误与第二个完全相同,只是这里的隐式转换(失败)是在返回值应该是 String 时返回 int

需要注意的一件有趣的事情是,如果代码中的整数为 0 而不是 10,则代码编译。原因是 0 可以隐式转换为地址(NULL 地址),这对于接受指针的构造函数来说是一个有效值。

String s;
s = 0;   // ok
s = '\0' // ok

【讨论】:

  • 有一个构造函数可以接受char。但它是明确的。
【解决方案2】:

作者正在记录编译器会因为没有转换或选择的转换标记为explicit 而给您错误的情况。如果有一个实际可行的案例,代码可能会更清晰:

class String{
  explicit String(int n);
  String(const char *p);
};
String  s1= ‘a’;     //error:  no implicit char->String conversion
                     // There is a combo implicit/explicit one...
                     // char (implicit) -> int (explicit) -> String

void f(String);

String g( )
{
   f(10);       //  error: no implicit int->String conversion
                //  (the String(int n) constructor is marked explicit).

   f("fred");   //  not an error: uses the String(const char *) constructor
                //  for an implicit conversion.

   f(String(10)); // not an error, explicitly calls the String(int n)
                  // constructor.

   return 10;   //  error:  no implicit int-> String conversion
}

【讨论】:

  • 绝对有明确的charString 转换。
  • @Ben Voigt - 哦,是的。字符 -> 整数 -> 字符串。 叹息我会解决的。
【解决方案3】:

【讨论】:

    【解决方案4】:

    作为附加信息。首先,为什么我们需要关心隐式转换?考虑以下场景。

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Test
    {
      public:
            Test ( int x);
            ~Test ();
            void print ();
            bool operator==(const Test &temp);
      private:
            int _x;
    };
    
    Test::Test (int x)
    {
      _x = x;
    }
    
    Test::~Test ()
    {
    }
    
    void Test::print ()
    {
     cout <<"_x : "<<_x<<endl;
    }
    
    bool Test::operator==(const Test & temp)
    {
        cout <<"Comparing "<<_x <<" With "<<temp._x<<endl;
        if (_x == temp._x) return 1;
        else
           return 0;
    }
    
    int main (int argc, char ** argv)
    {
       Test t1(10); //OK
       Test t2 = 10; // We intented
       t1.print ();  /* Excellent */
       t2.print ();
    
       /* What we do not intend is this : silent Conversion from int to Object */
       /* Hey man i forgot to mention t2, but mentioned '2' instead. */
    
       if ( t1 == 2 )
        cout <<"TRUE"<<endl;
       else
        cout <<"FALSE"<<endl;
    }
    

    它仍然编译,并将整数“2”转换为测试对象,这不是我的意图,而是比较 t1 和 t2。在单参数构造函数前面使用 'explicit' 关键字,可以避免这种静默转换。希望这会有所帮助!!!..

    explict test (int x);  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多