In deplorable style, this program attempts to use constructors to add 3 numbers and print the results but sadly nothing gets printed. Can you spot the flaw?

 #include <iostream>

int sum = 0;

 class Add
            Add( int a, int b, int c )  { sum += a; Add x(b,c); }
            Add( int a, int b )         { sum += a; Add x(b); }
            Add( int a )                { sum += a; Add x(); }
            Add(  )                       { std::cout << sum; sum = 0; }

int main()
    Add x( 21, 15, 9 );
    return 0;

To solve this problem, you can get help from static code analysis with PC-Lint Plus.
Just copy the code and try ist out in the free online demo:

Feel free to leave a comment about your solution.


Legal Notice

This text is the intellectual property of the author and is copyrighted by You are welcome to reuse the thoughts from this blog post. However, the author must always be mentioned with a link to this post!

Leave a Comment

Related Posts

Hey there!

Subscribe and get a monthly digest of our newest quality pieces to your inbox.