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
What is a lambda function c++?
How can you quickly find the number of elements stored in a static array?
Is it possible to write a c++ template to check for a function's existence?
What is a buffer c++?
What is setiosflags c++?
Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.
What are the uses of typedef in a program?
What is meant by a delegate?
What is operators in c++?
Explain the static member function.
What is the difference between cin.read() and cin.getline()?
What is a unnitialised pointer?
Can notepad ++ run c++?
What is c++ programming language?
Write a Program for find and replace a character in a string.