C++ assignment operator during declaration

When initializing an object at definition, it’s not using assignment even when the = syntax is used.

When you do:

Hero zak = foo;

it’s equivalent to:

Hero zak(foo);

Which is copy-initialization and as such invokes the copy-constructor.

The problem with

Hero bar = "HelloWorld";

is that it’s equivalent to:

Hero bar = Hero("HelloWorld");

which in turn is equivalent to:

Hero bar(Hero("HelloWorld"));

And since you don’t have a constructor for Hero("HelloWorld") it’s invalid.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top