zoukankan      html  css  js  c++  java
  • What does the explicit keyword mean?

    The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter.

    Here's an example class with a constructor that can be used for implicit conversions:

    class Foo
    {
    public:
      // single parameter constructor, can be used as an implicit conversion
      Foo (int foo) : m_foo (foo) 
      {
      }
    
      int GetFoo () { return m_foo; }
    
    private:
      int m_foo;
    };
    

    Here's a simple function that takes a Foo object:

    void DoBar (Foo foo)
    {
      int i = foo.GetFoo ();
    }
    

    and here's where the DoBar function is called:

    int main ()
    {
      DoBar (42);
    }
    

    The argument is not a Foo object, but an int. However, there exists a constructor for Foo that takes an int so this constructor can be used to convert the parameter to the correct type.

    The compiler is allowed to do this once for each parameter.

    Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call DoBar (42). It is now necessary to call for conversion explicitly with DoBar (Foo (42))

    The reason you might want to do this is to avoid accidental construction that can hide bugs.
    Contrived example:

    • You have a MyString(int size) class with a constructor that constructs a string of the given size. You have a function print(const MyString&), and you call print(3) (when you actually intended to call print("3")). You expect it to print "3", but it prints an empty string of length 3 instead.

    Suppose, you have a class String:

    class String {
    public:
        String(int n); // allocate n bytes to the String object
        String(const char *p); // initializes object with char *p
    };
    

    Now, if you try:

    String mystring = 'x';
    

    The character 'x' will be implicitly converted to int and then the String(int) constructor will be called. But, this is not what the user might have intended. So, to prevent such conditions, we shall define the constructor as explicit:

    class String {
    public:
        explicit String (int n); //allocate n bytes
        String(const char *p); // initialize sobject with string p
    };
    

    https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean
  • 相关阅读:
    单选框和复选框(radiobox、checkbox)
    三种alertconfirmprompt弹窗的处理方法
    iframe的切换
    python的class(类)中的object是什么意思?
    loadrunner12自带的机票预订服务,解决httpd: Could not reliably determine the server's fully qualified domain name 问题
    使用错误的用户名和密码也能运行通过
    win10删除IE某些文件导致不可用恢复的方法
    win10系统删除需要Trustedlnstaller权限的文件
    loadrunner各版本对应的ie浏览器版本
    vue之vue-router加深印象
  • 原文地址:https://www.cnblogs.com/Searchor/p/14024633.html
Copyright © 2011-2022 走看看