Home > C++, VC++ > explicit c++ keyword explained

explicit c++ keyword explained

A friend of mine was asking this question so quickly thought of explaining it to other doubtees. 😉

In one sentence, it prevents automagic/implicit conversions. Let me explain with an example…

First compile this sample using the explicit keyword, compile again with explicit keyword removed. First time you will get error, second time with the explicit keyword removed there will be no errors since the compiler converts “200” to Test(200) form (magic).

class Test
{
   public:
      Test() : m_Num( 0 )
      {}

      explicit Test( const int Num ) : m_Num( Num )
      {}

      Test& operator / ( const Test& Obj )
      {
         m_Num /= Obj.m_Num;
         return *this;
      }

   private:
      int m_Num;
};

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
   Test Obj1( 20 );
   // This will cause error C2679: binary '/' : no operator defined which takes
   // a right-hand operand of type 'const int'
   // (or there is no acceptable conversion)
   // Reason: Because the constructor that takes an int is declared as "explicit"
   Test ObjResult = Obj1 / 200;
   // Try this to avoid above error
   // This is fine, since we are "explicit"ly creating a Test and this is the purpose
   // of the explicit keyword, to prevent magic conversions
   Test ObjResultProper = Obj1/Test(200);
}

explicit does help us to be in control, it’s good to use explicit when you have arithmetic operators defined, will help prevent some nasty bugs.

A constructor that takes an argument can be called a conversion operator for converting that argument type to this class’ object. 🙂

Advertisements
  1. April 9, 2009 at 10:50 pm

    Thanks for the explanation!

    So, if I understand correctly, explicit prevents an object being assigned from an integer (or some other data type) as if you had called the object’s constructor that took that data type as an argument?

    Meaning without explicit the line
    Object obj(100);

    would be functionally the same as
    Object obj = 100;

    Am I correct in this, or did I miss the point here?

    Thank you. God Bless!

  2. July 16, 2008 at 1:31 pm

    Take a look at this code…

    class ExpTest
    {
    public:

    explicit ExpTest( int n ) : m_n( n )
    {}

    private:
    int m_n;
    };

    int main()
    {
    ExpTest ExpObj = 1; // Will give error if explicit is defined else will compile fine since the constructor acts as a conversion operator.
    }

  3. nikhila
    July 16, 2008 at 1:23 pm

    can u please give still more and simple example of code that uses explicit key word

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: