View Single Post
Old 01-28-2005, 09:03 PM   #19 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Page 12:

Initialisation versus Assignment

This page is just making sure we know what we are talking about. There is a distinct difference between those two in C++.

Assignment

Assignment occurs in such a way, that an object is created but NOT assigned a value to it straight away:
Code:
String A;
A = "Hi there baby.";
This innocent piece of code is less innocent then we might think.

Initialisation
Initialisation occurs in such a way, that an object is created and assigned a value to it straight away. Actually, this definition is so utterly bad, I can't bare it. Luckily we are going to cover this in detail as well. First we need samples of initialisation:
Code:
String A("Hi there baby");
//or
String B = "Hi there baby";
As you can see, both are one-liners.

A more serious analysis

If we observe line 2 in the first code snippet, and line 3 in the second code snippet...
A = "Hi there baby."; vs String B = "Hi there baby.";
... then we can see that C++ makes a fundamental difference between various versions of =. Math doesn't have this distinction, but C++ has.

The assignment code needs to be worked out better in detail. This is what happens:
Code:
String A;
This line calls your default constructor, a self defined parameter less constructor, or a constructor that acts like a conversion operator. Only the latter is implemented by us later. Then:
Code:
A = "Hi there baby.";
This line does two things: 1) it calls the constructor once again 2) it calls the overloaded operator=.
So the whole snippet will do this in our String class later:
1)Call -> (conversion) constructor
2)Call -> (conversion) constructor
3)Call -> operator=
And finally:
4)Call -> destructor

The double call to the constructor seems inefficient. It is. More on that in a bit. Let's see what happens when we implement the second snippet: initialisation.
Code:
String A = "Hi there baby";
1)Calls -> (conversion) constructor
That's it. Not even the destructor or operator+ needed to be called. Wow. And this:
Code:
String A("Hi there baby");
Calls -> (conversion) constructor.
Same thing. That's it.

Passing objects around

We are going to analyse what happens when we execute these two snippets (that do the same in the end):
Code:
String A("Hi baby.");
String B(" Wanna dance?");
String C;
C = A + B;
Or
Code:
String A("Hi baby.");
String B(" Wanna dance?");
String C(A + B); //OR: String C = A + B;
Obviously the output in both cases is: Hi baby. Wanna dance?
We have enough knowledge to calculate what happens.

The first snippet:

Calls -> (conversion) ctor
Calls -> (conversion) ctor
Calls -> (conversion) ctor
Calls -> Copy ctor
Calls -> operator=()
Calls -> destructor


The second snippet:

Calls -> (conversion) ctor
Calls -> (conversion) ctor
Calls -> Copy ctor


Yet, once again, both snippets do the same thing. So the conclusion we can draw is that initialisation is more efficient. But we need both!

Basically here ends a recap on initialisation versus assignment. True, there is more to tell about it. But it won't be relevant to the main topic. For the sake of easy recognition: initialisations are the one-liners.

The next page will build the very foundations of our String class.

Quote:
I like the italics. Adjust!
__________________
Valmont is offline   Reply With Quote