C++ | Templates | Question 5
Output of following program? Assume that the size of char is 1 byte and size of int is 4 bytes, and there is no alignment done by the compiler.
#include<iostream> #include<stdlib.h> using namespace std; template < class T, class U> class A { T x; U y; static int count; }; int main() { A< char , char > a; A< int , int > b; cout << sizeof (a) << endl; cout << sizeof (b) << endl; return 0; } |
(A)
6 12
(B)
2 8
(C) Compiler Error: There can not be more than one template arguments.
(D)
8 8
Answer: (B)
Explanation: Since count is static, it is not counted in sizeof.
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.