Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » The Rule Of Three
      by Valmont
 Page 4 of 17 
< Previous     Next >

(Login to remove green text ads)
Initialisation vs Assignment
Initialization Versus Assignment


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:

Calls -> (conversion) constructor
Calls -> (conversion) constructor
Calls -> operator=
Calls -> destructor
Calls -> 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: initialization.
Code:
String A = "Hi there baby";
Calls -> (conversion) constructor
Calls -> destructor

That's it. And now the second way to initialize:
Code:
String A("Hi there baby");
Calls -> (conversion) constructor.
Calls -> destructor

Same thing.

Passing Objects Around

We are going to analyze 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); [color=#008800]//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 -> operator+()
Calls -> Copy ctor
Calls -> operator=()
Calls -> destructor
Calls -> destructor
Calls -> destructor
Calls -> destructor


The second snippet:

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


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.




 
 Page 4 of 17 
< Previous     Next >

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting