How do you initialize a class member,
class x {
const int i;
};
Answer Posted / jp
const data members must be initialized using Initializer List. In the following example, “t” is a const data member of Test class and is initialized using Initializer List.
#include<iostream>
using namespace std;
class Test {
const int t;
public:
Test(int t):t(t) {} //Initializer list must be used
int getT() { return t; }
};
int main() {
Test t1(10);
cout<<t1.getT();
return 0;
}
/* OUTPUT:
10
*/
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Describe the advantages of operator overloading?
What are the operators in c++?
What are guid? Why does com need guids?
What do you understand by pure virtual function? Write about its use?
What is guard code in c++?
Explain what is oop?
What is the use of namespace std in C++?
Is nan a c++?
What is class definition in c++ ?
What is c strings syntax?
What is iterator c++?
Can I learn c++ without c?
What is the difference between an array and a list?
How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?
What is ifstream c++?