Q:
When do we use the Initializer List in C++?
belongs to collection: C++ Interview Questions For Experienced
C++ Interview Questions For Experienced
- Can a constructor throw an exception? How to handle the error when the constructor fails?
- What is the initializer list in C++?
- When do we use the Initializer List in C++?
- What is a copy constructor in c++?
- When are copy constructors called in C++?
- Why copy constructor take the parameter as a reference in C++?
- Why copy constructor argument should be const in C++?
- Can one constructor of a class call another constructor of the same class to initialize this object?
- Can a copy constructor accept an object of the same class as a parameter, in place of reference of the object? If No, why not possible?
- Are Constructors and destructors can declare as const?
- Can we make a copy constructor private in c++?
- Can you explain the order of execution in the constructor initialization list in c++?
- What is the conversion constructor in c++?
- What is the difference between a copy constructor and an overloaded assignment operator?
- What is the conversion operator in C++?
- When do we need to write a user-defined destructor?
- Why a class has only one destructor in c++?
- Can we have a virtual destructor in C++?
- When to use virtual destructors in c++?
- Can we have a virtual constructor in C++?
- Can you change ‘this pointer’ of an object to point to different objects in c++?
- Can you modify the ‘this pointer’ type in c++?
- Can I use realloc() on pointers allocated via new in c++?
- Why should C++ programmers minimize the use of ‘new’?
- Can I free() pointers allocated with new in c++?
- Is there any problem with the following c++ code : char*a=NULL, char& p = *a?
- Can I delete pointers allocated with malloc() ijn c++?
- How to call a non-const member function from a const member function in C++?
- When should I use references, and when should I use pointers?
- How virtual functions are implemented in C++?
- Is there a separate vtable for each object?
- Can virtual functions be inlined in c++?
- Can a virtual function is called inside a non-virtual function in C++?
- What is a pure virtual function in C++?
- What is difference between Virtual function and Pure virtual function in C++?
- Why is a pure virtual function initialized by 0 in c++?
- Can we access private data members of a class without using a member or a friend function?
- Can virtual functions be private in C++?
- What is an abstract class in c++?
- Write down some important points related to abstract function in c++?
- What is the difference between a concrete class and an abstract class in c++?
- What is a template function in c++?
- What is the difference between function overloading and templates in c++?
- Can we combine C and C++ code?
- How can I include a non-system C header file in my C++ code?
- What is the effect of extern “C” in C++?
- Why do C++ compilers need name mangling?
- What is typecasting in c++?
- When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?
- How does the compilation/linking process work in c++?
- How to make a C++ class whose objects can only be dynamically allocated?
- What does the explicit keyword mean?
- How do you access the static member of a class in c++?
- Distinguish between shallow copy and deep copy in c++?
- Friend class and function in C++?
- What is the Diamond problem? How can we get around it in c++?
- Why virtual functions cannot be static in C++?
- What is the “mutable” keyword in C++?
- How to handle the exception in C++?
- What is a Memory Leak?
- Why static functions cannot access non-static variables in c++?
- What is a dangling pointer in c++?
- STL Containers – What are the types of STL containers?
- What are the functions of the scope resolution operator in c++?
- Write a program that describes the safe way to access one object to another in C++?
- Could you write an example code that describes the use of explicit keyword in c++
- Why is “using namespace std;” considered bad practice in c++?
- Why can templates only be implemented in the header file in c++?
- Do all virtual functions need to be implemented in derived classes in c++?
- Do all pure virtual functions need to be implemented in derived classes in c++?
- How to call a parent class function from a derived class function in c++?
- How to access members of the namespace in different files?
- How to convert a std::string to const char* or char* in c++?
Answer:
In the above question, we had seen, what is the initializer list in C++. Now let us see the situation where we have to use the Initializer List in C++.
1. In the initialization of reference members:
A reference member must be initialized using Initializer List.
Output:
m_rData is 27
m_rData is 6
------------------------------------------------------------------------------------------
2. In the initialization of non-static const data members:
const data members must be initialized using Initializer List.
Output: m_data is 27
---------------------------------------------------------------------------------------------------------------
3. In the initialization of member objects which do not have default constructor:
See the below example, an object “a” of class “A” is a data member of class “B”, and “A” doesn’t have a default constructor. Initializer List must be used to initialize “a”.
Output:
A’s Constructor called: Value of i: 10
B’s Constructor called
------------------------------------------------------------------------------------------------------
4. In the initialization of base class members :
You have to initialize the base class members using the initialization list.
Output:
A’s Constructor called: Value of i: 10
B’s Constructor called
---------------------------------------------------------------------------------------------------
5. When the constructor’s parameter name is the same as the data member:
If the constructor’s parameter name is the same as the data member name then the data member must be initialized either using this pointer or Initializer List.
Output: 27
-------------------------------------------------------------------------------------------------------
6. To increase performance:
It is better to initialize all class variables in the Initializer List instead of assigning values inside the constructor body.
need an explanation for this answer? contact us directly to get an explanation for this answer