Explicit C++ Explicit keyword parsing

  • 2020-04-02 01:40:38
  • OfStack

The explicit keyword modifies the constructor of the class to indicate that the constructor is explicit, as opposed to the implicit keyword.
First of all, this keyword can only be used for constructor declarations inside the class, not for function definitions outside the class, which cannot be implicitly converted.


    class gxgExplicit  //Class with no keyword explicit
    {
      public:
          int _size;
         gxgExplicit(int size)
        {
           _size = size;
        }
     };

Here is the call
    GxgExplicit gE1 (24).         // there is no problem with that
    GxgExplicit gE2 = 1;         // there is no problem with that
    GxgExplicit gE3;                 // this will not work, there is no default constructor
    GE1 = 2;                                 // there is no problem with that
    GE2 = 3;                                 // there is no problem with that
    GE2 = gE1;                             // there is no problem with that

But if gxgExplicit is explicitly changed to Stack, and our _size represents the size of the Stack, then the second sentence of the call is nonsensical and confusing. This is not a form that the code reader can understand and accept, although it is legal (the compiler can compile). This is because the compiler has implicit conversion by default, and you type gE2 = 1 and it will compile to the same result as the first sentence. So, explicit comes in handy. The modified code is:


class gxgExplicit
    {
      public:
           int _size;
           explicit gxgExplicit(int size)
           {
             _size = size;
           }
    };

Continue with the above call:
    GxgExplicit gE1 (24).         // there is no problem with that
    GxgExplicit gE2 = 1;         // this will not work, keyword to cancel the implicit conversion
    GxgExplicit gE3;                 // this will not work, there is no default constructor
    GE1 = 2;                                 // this will not work, keyword to cancel the implicit conversion
    GE2 = 3;                                 // this will not work, keyword to cancel the implicit conversion
    GE2 = gE1;                             // this is not possible, the keyword cancels implicit conversion, unless the class implements overloading of the operator "=".

This is the compiler (vs2005) saying: cannot convert from 'int' to 'gxgExplicit'.
From here it can be seen that the purpose of this keyword is to mask the compiler's implicit conversion function.
A note on MSDN describes the fact that implicit conversions are automatically cancelled when constructor arguments exceed two. For example,


    class gxgExplicit
    {
      private:
         int _size;
         int _age;
      public:
          explicit gxgExplicit(int age,int size)
         {
            _age = age;
            _size = size;
         }
     };

This is no keyword effect is the same. That's the equivalent of having this keyword.
There is an exception to this: only one of the arguments must be entered, and the rest are arguments with default values.

class gxgExplicit
{
private:
   int _size;
   int _age;
public:
   explicit gxgExplicit(int age,int size = 0)
   {
      _age = age;
      _size = size;
   }
};
class gxgExplicit
{
private:
   int _size;
   int _age;
int _hight;
public:
   explicit gxgExplicit(int age,int size = 0)
   {
      _age = age;
      _size = size;
      _hight = hight;
   }
};


Related articles: