隐式转换

发布于 2024-10-21 01:48:00 字数 389 浏览 1 评论 0原文

在下面的例子中,作者对隐式转换做了几点注释。您能否更详细地解释一下这些评论,我对这些评论不太清楚。谢谢。

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
}

In the following example, the author made several comments on the implicit conversion. Can you explain more detail on these comments, which are not very clear to me. Thanks.

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一场信仰旅途 2024-10-28 01:48:00

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

例如:

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

The String class has two constructors; one for constructing a String from an int and one for constructing a String from a const pointer to a char. These two constructors are hence also conversion functions, because they really convert one type into another. The first constructor, however, is an explicit constructor. While the second constructor allows for implicit conversion from a pointer-to-char to a String, the first constructor requires you to ask for the conversion explicitly.

For example:

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

The first error comment simply says that there is no constructor for converting a char to a String. Again, we have only two constructors: one for converting an int, the other a const pointer to char.

The second error talks about passing as parameter an int to a function that requires a String. This implies that the function must construct a String from an int implicitly. This can't be done because the relevant constructor is explicit. If you'd construct a String from an int and then pass that String to the function all would be well.

The third error is exactly the same as the second, only here the implicit conversion (which fails) is at returning an int when the return value should be a String.

One interesting thing to note is that the code would compile if the integer in your code would be 0 and not 10. The reason is that 0 can be implicitly casted to an address (the NULL address), and that is a valid value for the constructor that takes a pointer.

String s;
s = 0;   // ok
s = '\0' // ok
蓝天白云 2024-10-28 01:48:00

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

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
}

The author is documenting cases in which the compiler will give you an error because either there is no conversion, or the conversion selected is marked explicit. The code might be clearer with a case that would actually work:

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
}
橘虞初梦 2024-10-28 01:48:00

这些注释基本上表明编译器无法知道如何与 String 数据类型进行转换。

请参阅:http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr383.htm

另外: http://www.glenmccl.com/tip_023.htm

The comments basically say that the compiler has no way of knowing how to convert to/from the String datatype.

See: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr383.htm

Also: http://www.glenmccl.com/tip_023.htm

音栖息无 2024-10-28 01:48:00

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

#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。在单参数构造函数前面使用“显式”关键字可以避免这种静默转换。希望这有帮助!

explict test (int x);  

As addition info. first of all why we need to bother about the implicit conversion? Consider the following scenario.

#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;
}

It still compiled, and converted the integer '2' into Test Object, which was not my intention, but to compare t1 and t2. Using 'explicit' keyword infront of the single argument constructor, avoids such silent conversion. Hope this helps!!!..

explict test (int x);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文