wrote:
> From page 477 of 4th edition of C++ Primer.
> "Direct-initialization directly invokes the constructor matched by the
> arguments. Copy-initialization always involves the copy-constructor."
>
> It occurs to me that the "constructor matched by the arguments" might
> be the copy-constructor. In this case, I would think copy-
> initialization and direct-initialization are exactly the same. My
> concern (and reason for this posting) is that I haven't seen anyone
> say the same thing.
Well, I believe it has been said here many times. According to the
language specification, when the object type and the initializer type
are the same (ignoring any CV-qualification), both initializations works
in exactly the same way (is copy-initialization follows exactly the same
algorithm as direct-initialization).
> For example, does std::string null_book = "99"; mean the same
> as std::string null_book("99"); ?
No, of course not. But what does this have to do with your original
question? Your question was about situation when copy constructor is
selected by direct-initialization. In this example direct-initialization
_does_ _not_ select copy constructor. Instead, it will use conversion
constructor from 'const char*'. So, what is this example is doing here?
> (On my compiler, they behave the same way.)
I'm not sure what you mean by "the same" here, but if they indeed behave
exactly the same way, this might be the effect of compiler optimization.
From the abstract C++ point of view the first version calls copy
constructor (just like you said above), while the second does not. I.e.
they don't behave the same way.
> And would the behaviour always be the same if std::string
> is replaced by another class?
It is hard to understand what you are trying to ask, since the example
you are referring to does not match the original question you were
asking. Please, clarify your question.
--
Best regards,
Andrey Tarasevich