zoukankan      html  css  js  c++  java
  • C++中 explicit的用法

    explicit 是避免构造函数的参数自动转换为类对象的标识符

    #include <iostream>
    using namespace  std;
    
    class A
    {
    public:
    
    	 explicit A(int a)
    	{
    		cout<<"创建类成功了!"<<endl;
    
    	
    	}
    	
    	
    
    };
    int main()
    {
    
    	A a=10;
    	return 0;
    }

    上面的代码编译不成功,原因是当显式地定义了一个带一个参数的构造函数( 带explicit),必须要显示地调用构造函数,

    A a(10);

    如果不加 explicit的话

    A a=10;

    实际的转换过程如下:
    相当于直接调用A(10);

    (1)

    explicit

    此关键字只能对用户自己定义的对象起作用,不对默认构造函数起作用
    此关键字只能够修饰构造函数。而且构造函数的参数只能有一个。。

    (2)何时用explicit

    当我们不希望自动类型转换的时候用,其实标准库好多构造函数都是explicit的

    比如说vector <int> ivec(10);  //这种定义看起来一目了然

    不能写成vector <int> ivec=10;//此种定义让程序员感到疑惑

    (3)何时不用explicit

    当我们需要隐式转换的时候

    比如说String类的一个构造函数

    String(const char*);

    定义成这样的好处,在需要隐式转化的时候编译器会自动地帮我们转换,标准库里面的String就是一个好的证明。

    具体来说:

    我们可以这样String str="helloworld";//直接调用构造函数

    String str="hello"+str+"world";

    调用重载的+操作符号,此过程相当于:
    String temp("hello"); //调用构造函数

    String str=temp+str;

    String t("world");//调用构造函数

    String str=str+t;


    转载:https://blog.csdn.net/acdnjjjdjkdckjj/article/details/5644573#commentBox

  • 相关阅读:
    [LeetCode]Search a 2D Matrix
    [LeetCode]Trapping Rain Water
    [LeetCode]Container With Most Water
    [LeetCode]Permutations
    [LeetCode]Missing Number
    [LeetCode]Set Matrix Zeroes
    [LeetCode]Find Peak Element
    [LeetCode]Balanced Binary Tree
    [LeetCode]Spiral Matrix II
    [LeetCode]Spiral Matrix
  • 原文地址:https://www.cnblogs.com/oneDongHua/p/14264066.html
Copyright © 2011-2022 走看看