1. What are the special member functions that the compiler will write for you if
ID: 3720729 • Letter: 1
Question
1. What are the special member functions that the compiler will write for you if you don’t? (Hint: There are 6 of them.)
2. What do the default copy constructor and copy assignment operator do?
3. What do the default constructor and destructor do?
4. What is the difference between copy assignment and copy initialization?
5. How do you prevent the compiler from generating a special member function?
6. What is the difference between moving and copying?
7. When will the compiler use move assignment or initialization rather than copy assignment or Initialization?
8. There are situations in which you would like to allow objects to be moved, but not copied. What do you need to do to prevent copying while allowing objects to be moved?
Explanation / Answer
1)
a) default constructors
b)copy constructors
c)copy assignment constructors
d)destructor
e)move constructor
f)move assignment constructor
2) default copy constructor will initialize the parameters of an object with the parameters of given object.
Here is the example:
obj o1= o2;
here o2 parameters will be copied to o1 parameters
The default copy constructor will be called only when there is no user-defined available
the assignment operator is called when an already initialized object is assigned a new value from another existing object. And performs the same operation of copying the parameters mentioned above
o2 = o3; // calls assignment operator, same as "t2.operator=(t1);"
Test t3 = t1; // calls copy constructor, same as "Test t3(t1);"
3) a default constructor will be called by a compiler when there is no user-defined available.
And it is called by compiler the object of a class is created. There will no code in a default constructor a default constructor is required to do certain initialization of class internals. It will not touch the data members or data types like array, structures, etc..
If a class is derived class the compiler needs to insert code to call the default constructors of the base class
Destructor deletes an object. Means it will deallocate the memory assigned to it and it will free the resources that the object has acquired during its lifetime
4) In this case ::T t = x;
Copy initialization constructs an implicit conversion sequence: It tries to convert x to an object of type T
Where copy assignment will be called when already defined t object values we have to change again.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.