This page is just making sure we know what we are talking about. There is a distinct difference between initialization and assignment 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 small piece of code is less innocent then we might think.
Initialization
Initialization 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 initialization:
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. Then:
Code:
A = "Hi there baby.";
This line does two things:
It calls the constructor once again.
It calls the overloaded operator=().
So the whole snippet will do this in our String class later:
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: initialization.
Yet, once again, both snippets do the same thing. So the conclusion we can draw is that initialization is more efficient. But we need both!
Basically here ends a recap on initialization 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: initializations are the one-liners.
The next page will build the very foundations of a custom String class.