C++ Object Instantiation VS Assignment

C++ Object Instantiation vs Assignment

TestClass t;

calls the default constructor.

TestClass t = TestClass();

is a copy initialization. It will call the default constructor for TestClass() and then the copy constructor (theoretically, copying is subject to copy elision). No assignment takes place here.

There's also the notion of direct initialization:

TestClass t(TestClass());

If you want to use the assignment operator:

TestClass t;
TestClass s;
t = s;

Meanings of declaring, instantiating, initializing and assigning an object

Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name.

int a; //a is declared

Instantiate - Instantiating a class means to create a new instance of the class. Source.

MyObject x = new MyObject(); //we are making a new instance of the class MyObject

Initialize - To initialize a variable means to assign it an initial value.

int a; //a is declared
int a = 0; //a is declared AND initialized to 0
MyObject x = new MyObject(); //x is declared and initialized with an instance of MyObject

Assigning - Assigning to a variable means to provide the variable with a value.

int a = 0; //we are assigning to a; yes, initializing a variable means you assign it a value, so they do overlap!
a = 1; //we are assigning to a

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.,

Constructor initialization Vs assignment

What might go wrong if we perform assignment instead of initialization?

Some class types (and also references and const objects) can't be assigned; some can't be default-initialised; some might be more expensive to default-initialise and reassign than to initialise directly.

Doesn't the compiler internally performs assignment in case of test1() constructor? If no then how are these initialized?

In the case of primitive types like int, there is little or no practical difference between the two. Default-initialisation does nothing, and direct-initialisation and assignment both do essentially the same thing.

In the case of class types, default-initialisation, assignment and direct-initialisation each call different user-defined functions, and some operations may not exist at all; so in general the two examples could have very different behaviour.

What happens when you initialize a C++ object by assignment?

Car has an implicitly declared copy-ctor, which is used here as the ctor-call cannot be elided (it is not initialized with a pr-value), nor is move-construction possible (it is not an xvalue).

That implicitly-declared copy-ctor does member-wise copy-construction.

What is the difference Between Assignment and Creating Instance of String in C#?

C# compiler optimizes it so the same literals point to the same string instance

MSDN:

The intern pool conserves string storage. If you assign a literal
string constant to several variables, each variable is set to
reference the same constant
in the intern pool instead of
referencing several different instances of String that have identical
values.



Related Topics



Leave a reply



Submit