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
How do I tokenize a string in c++?
Does std endl flush?
Define pure virtual function?
Why is it necessary to use a reference in the argument to the copy constructor?
Differentiate between the manipulator and setf( ) function?
Define a conversion constructor?
What is doubly linked list in c++?
What operator is used to access a struct through a pointer a) >> b) -> c) *
What is the default access level?
What is abstraction in c++ with example?
What does it mean to declare a member variable as static?
Is c++ fully object oriented?
If dog is a friend of boy, is boy a friend of dog?
What are guid? Why does com need guids?
Evaluate the following expression as C++ would do :8 * 9 + 2 * 5 a) 82 b) 79 c) 370 d) list