Unofficial Hacker News companion — get notified on comment replies, and more.

In C++, = default and {} are not the same constructor (opens external site)

131 points by surprisetalk 6 days ago | lemire.me | 202 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (1)

  1. Right, but that’s a relatively rare problem, and doesn’t usually result in crashes.

    More importantly, you have exactly the same problem if you overload a regular method that way. So it’s a problem with overloading, not with constructors.

    > How do you construct a MyVector of two items?

        new MyVector(new int[] { 23, 42 })
    
    The correct solution, of course, is to provide named static constructor methods instead (e.g. `MyVector.ofRange(start, end)`).

    I do agree that support for named actual constructor methods would be nice. But they would still be constructors.