C++ | Constructors | Question 9
Predict the output of following program.
#include<iostream> #include<stdlib.h> using namespace std; class Test { public: Test() { cout << "Constructor called"; } }; int main() { Test *t = (Test *) malloc(sizeof(Test)); return 0; } |
chevron_right
filter_none
(A) Constructor called
(B) Empty
(C) Compiler Error
(D) Runtime error
Answer: (B)
Explanation: Unlike new, malloc() doesn’t call constructor (See this)
If we replace malloc() with new, the constructor is called, see this.
Quiz of this Question
Recommended Posts:
- C++ | Constructors | Question 12
- C++ | Constructors | Question 5
- C++ | Constructors | Question 6
- C++ | Constructors | Question 7
- C++ | Constructors | Question 4
- C++ | Constructors | Question 8
- C++ | Constructors | Question 14
- C++ | Constructors | Question 3
- C++ | Constructors | Question 17
- C++ | Constructors | Question 15
- C++ | Constructors | Question 16
- C++ | Constructors | Question 17
- C++ | Constructors | Question 13
- C++ | Constructors | Question 2
- C++ | Constructors | Question 1



