Initialisation and assignment
Oh my. Initialization and assignment. Well, that's confusion for sure!
To initialize is to make ready for use. And when we're talking about a variable, that means giving the variable a first, useful value. And one way to do that is by using an assignment.
So it's pretty subtle: assignment is one way to do initialization.
Assignment works well for initializing e.g. an int
, but it doesn't work well for initializing e.g. a std::string
. Why? Because the std::string
object contains at least one pointer to dynamically allocated memory, and
if the object has not yet been initialized, that pointer needs to be set to point at a properly allocated buffer (block of memory to hold the string contents), but
if the object has already been initialized, then an assignment may have to deallocate the old buffer and allocate a new one.
So the std::string
object's assignment operator evidently has to behave in two different ways, depending on whether the object has already been initialized or not!
Of course it doesn't behave in two different ways. Instead, for a std::string
object the initialization is taken care of by a constructor. You can say that a constructor's job is to take the area of memory that will represent the object, and change the arbitrary bits there to something suitable for the object type, something that represents a valid object state.
That initialization from raw memory should ideally be done once for each object, before any other operations on the object.
And the C++ rules effectively guarantee that. At least as long as you don't use very low level facilities. One might call that the C++ construction guarantee.
So, this means that when you do
std::string s( "one" );
then you're doing simple construction from raw memory, but when you do
std::string s;
s = "two";
then you're first constructing s
(with an object state representing an empty string), and then assigning to this already initialized s
.
And that, finally, allows me to answer your question. From the point of view of language independent programming the first useful value is presumably the one that's assigned, and so in this view one thinks of the assignment as initialization. Yet, at the C++ technical level initialization has already been done, by a call of std::string
's default constructor, so at this level one thinks of the declaration as initialization, and the assignment as just a later change of value.
So, especially the term "initialization" depends on the context!
Simply apply some common sense to sort out what Someone Else probably means.
Cheers & hth.,
C++: initialize vs assignment?
The curly braces is part of uniform initialization which was added with the C++11 standard.
Using
int value {1};
is equivalent to
int value = 1;
There's some differences between using curly braces and "assignment" syntax for initialization of variables, but in this simple case they're equal.
Initialization vs assignment
TLDR:
{ // declaration (hoisted)
// Temporal deadzone
let foo; // declaration and initialization to undefined
foo = 1; // assignment
}
A bit longer:
Declaration
Declaring a variable means that we reserve the identifier at the current scope. In javascript declarations are hoisted, that means that it gets declared when the scope the variable is in gets visible (the block it is in gets executed). However you cannot access that variable now as it is in
The temporal deadzone
This is a specific part of the code that is between the beginning of the scope and the initialization. Trying to access the variable here results in an error.
Initialization
The initialization takes place in the line were you declared the variable. It will assign a value to the variable and will make it available for access. This for example:
let foo;
will initialize foo
to undefined
,
let foo = 2;
will initialize foo
to 2
.
Assignment
...just means that you change the value of a variable. All assignments in javascript use =
. The initialization is basically just the first assinment.
The explanation above does not apply to variables declared with var
, so just don't use var
to avoid confusion :)
Java: define terms initialization, declaration and assignment
assignment: throwing away the old value of a variable and replacing it with a new one
initialization: it's a special kind of assignment: the first. Before initialization objects have null
value and primitive types have default values such as 0
or false
. Can be done in conjunction with declaration.
declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.
Related Topics
What Is the Meaning and Usage of _Stdcall
C++ Getters/Setters Coding Style
How Does the Range-Based for Work for Plain Arrays
Is There a Linq Library for C++
Vector or Map, Which One to Use
What Is the Simplest Way to Convert Array to Vector
Are Parentheses Around the Result Significant in a Return Statement
Dead Code Detection in Legacy C/C++ Project
Common Array Length MACro for C
Does the C++ Volatile Keyword Introduce a Memory Fence
Are There Any Better Methods to Do Permutation of String
Why Can't I Capture This By-Reference ('&This') in Lambda
How Does the Custom Deleter of Std::Unique_Ptr Work
Compile the Python Interpreter Statically
How to Generate the Audio Spectrum Using Fft in C++
Qgraphicsview Zooming in and Out Under Mouse Position Using Mouse Wheel