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
A mXn matrix is given and rows and column are sorted as shown below.Write a function that search a desired entered no in the matrix .with minimum complexity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
What is the use of namespace std in C++?
Why do we use string in c++?
Differentiate between an external iterator and an internal iterator? What is the advantage of an external iterator.
When do we use copy constructors?
Explain the register storage classes in c++.
Explain the purpose of the keyword volatile.
What are the vectors in c++?
What are the different types of comments allowed in c++?
Explain differences between new() and delete()?
Is c++ used anymore?
What is the difference between public and private data members?
Describe public access specifiers?
What does n mean in c++?
How do you write a function that can reverse a linked-list?