Quick Q: Why assigning a value is calling the implicit constructor?

Save to:
Instapaper Pocket Readability

Quick A: Because the default assignement operator knows only the type of your object, which it can get be calling the constructor.

Recently on SO:

What is the point of calling constructor with implicit conversion instead of assignment operator after an object is initalized?

There is no implicit CBox::operator=(double), so box = 2.0; has to create a temporary CBox object. It's equivalent to box = CBox(2.0);.

Making your constructor explicit disallows the implicit conversion from double to CBox, so no appropriate assignment operator exists, and you get a compile error.

Add a Comment

You must sign in or register to add a comment.

Comments (0)

There are currently no comments on this entry.