Applying explicit on your constructors

I am not sure how many C++ developers out there use the explicit keyword or are even aware of its existence. It was only today that I actually took a look at what it does and I’d like to thank my friend Rama Krishna for some vauable pointers (not the C/C++ kind) he gave me. Before I tell you what explicit does, lemme show you some code :-

class A
{
    friend class B;
public:
    A(int q) : _y(q)
    {
    }
private:
    int _y;
};

class B
{
public:
    B(A a) : _x(a._y)
    {
    }
    void Show()
    {
        printf("%d\r\n",_x);
    }
private:
    int _x;
};

Okay, nothing fancy about that code, eh? Now, take a look at this :-

int _tmain()
{
    B b = 17;
    b.Show();
    return 0;
}

I bet that looks pretty confusing for people who are not familar with conversion constructors. Basically, what happens is that the B b = 17 is considered as B b(17) and this is taken as B b(A(17))! Pretty crazy it might seem, but apparently, lot of guys have been using this style for years. (Thanks to Mike Dunn for pointing out an error in my original explanation; the error has been corrected)

The explicit keyword is used to specify that the constructor cannot take part in implicit conversions. Just change your code as follows :-

class A
{
    friend class B;
public:
    explicit A(int q) : _y(q)
    {
    }
private:
    int _y;
};

class B
{
public:
    explicit B(A a) : _x(a._y)
    {
    }
    void Show()
    {
        printf("%d\r\n",_x);
    }
private:
    int _x;
};

Now you’ll have to explicitly write B b(A(17)) or else get a compiler error.

BTW, just to verify if I am the only idiot in town who had never heard of this stuff, have any of you guys ever used this weird conversional syntax when constructing objects?

Advertisements
Posted in C++

14 thoughts on “Applying explicit on your constructors

  1. You still need to write code without those underscores 😉
    I knew about this syntax but have never used or seen this at production code so far.

  2. Likewise, I know about it and have played around with it.
    However as a matter of efficiency in using it I’m not sold.

  3. I’ve fallen into the trap of accidentally executing a constructor I didn’t mean to. As a result of that I found “explicit” and have used it a few times since. I’ve done a lot of C++ programming and stuff like this still catches me out.

  4. Again, Knowing this kind of crap code does save you from other sloppy joe’s design who left because he/she didn’t do a good job making his or her code readable.

    You should make a point here that such practice is not a good way to make you code cool. You might be pretty happy by your smart-a** code design, But a lot of people would hate you for it.

    Anyways, I don’t want to attack anyone, all I want is making a point, making your code clear and readable probably is better.

    By the way, I don’t think this code can survive code review with your colleague.

  5. The “explicit” keyword is one I use extensively these days, although as you say very few people know about it.

    “mutable” (useful in WTL apps using mutexes in const methods) and “volatile” (indicating a var which can change without warning, possibly as the result of a hardware interrupt) are two others that tend to be overlooked.

  6. One day he went along to his parents house and for the table there was
    obviously a piece of a tablet from temple that have been destroyed.
    These thin, light computers can fit nearly anywhere.
    Until ‘microsoft’ can reply these questions, after which men and women should have
    to carry off of on the choice to trade apple ipad tablet on the web.

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