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
Explain how a pointer to function can be declared in C++?
Explain the virtual inheritance in c++.
What does 7/9*9 equal ? a) 1 b) 0.08642 c) 0
What do you mean by vtable and vptr in c++?
Is it possible for the objects to read and write themselves?
What do you mean by persistent and non persistent objects?
What is buffer and example?
What is difference between array and vector in c++?
What is binary search in c++?
How to demonstrate the use of a variable?
the maximum length of a character constant can be a) 2 b) 1 c) 8
What are single and multiple inheritances in c++?
What is oops in c++?
Can we use pointers in c++?
Explain what are single and multiple inheritances in c++?