* Hattuari <su******@setid ava.kushan.aa> schriebt:
Alf P. Steinbach wrote:
* Hattuari <su******@setid ava.kushan.aa> schriebt: >> Where can it be used? How is it different from re=r? How should I
>> read in in English?
>
> FAQ: <url: http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.6>.
That doesn't seem to discuss the difference between the two forms
presented in my question.
It does or should -- it is the section of the FAQ dedicated to that
question.
My question was actually subtler than what is addressed in the FAQ. The FAQ
item has to do with the difference between the two approaches to
constructing the Complex class. I guess I wasn't clear enough since a few
people seem to have assumed I was asking the question addressed in the
item. What I really wanted to know is what exactly does double re; re(r);
mean? I believe the answer is that all fundamental type in C++ are
actually objects. I believe he states as much in Chapter 10. I can't find
it right now. I guess Stroustrup didn't think it important enough to
warrent more than a passing comment.
Well, I don't have the book (except the first edition, long since outdated).
But here's the technical background.
(1)
When T is a type name, the expression
T( ... arguments, if any )
is a value of type T initialized with the given actual arguments as
construction arguments.
When T is a built-in type there is no actual constructor, but the
allowed syntax is _as if_ there were default constructor so that e.g.
int()
means the value 0 of type int. And also _as if_ there were a copy
constructor.
(2)
The notation '=' is a bit more difficult because it has two different
meanings.
First, in declarations '=' can be used instead of constructor notation,
when the constructor takes exactly one argument. In this situation '='
stands for initialization. Initialization is different from assignment
in that with initialization there is no prior value (e.g., no dynamically
allocated memory to be deallocated), whereas with assignment it is assumed
that there is a prior value (e.g., deallocation may have to be done).
For example, in the variable declaration
int x = 7;
'=' is a short notation for initialization, equivalent to
int x( 7 );
using the 'as if' int copy constructor.
(3)
Outside of declarations '=' means assignment, which is different from
initialization in that it is assumed that there is a prior value. For
example,
std::string s = "Hi";
is a pure initialization with no deallocation, equivalent to
std::string s( "Hi" );
whereas
std::string s;
s = "Hi";
is a default initialization (possibly with dynamic allocation) followed
by an assignment (which possibly may have to deallocate something).