Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

Which is the best c++ compiler?

988


What are punctuators in c++?

1085


Explain how overloading takes place in c++?

969


What are the advantages of using friend classes?

1080


What is c++ library?

989


What are the various compound assignment operators in c++?

932


Describe exception handling concept with an example?

983


Why do we use classes in programming?

985


What is the difference between method overloading and method overriding in c++?

1080


Is c++ a float?

1010


Arrange Doubly linked list in the ascending order of its integral value and replace integer 5 with 7?

4068


Differentiate between the manipulator and setf( ) function?

1092


What are manipulators used for?

1035


Difference between declaration and definition of a variable.

1076


What is lvalue?

1129