0

Actually I don't know how to define this idioms.

In some code i have red something like:

ClassWithAMessage c = "This is the message";

where i expected to read:

ClassWithAMessage c("This is the message");

I don't know how to reproduce this behavior, can someone provide some information or a toy example?

Aslan986
  • 9,984
  • 11
  • 44
  • 75
  • 1
    See http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati – hmjd Sep 17 '12 at 16:07

1 Answers1

5
ClassWithAMessage c = "This is the message";

is copy initialization. A copy constructor must be available for this to work. First, a temporary ClassWithAMessage is constructed using the conversion constructor from "This is the message". The temporary is then used with the copy constructor to construct c. This is subject to copy elision (the temp might not be there).

ClassWithAMessage c("This is the message");

is direct initialization. The conversion constructor is used directly.

Not really idioms, just different ways to construct an object.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    In C++11 if a move constructor is available it will be used instead of the copy constructor for the first statement. – xception Sep 17 '12 at 16:17