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

What are pointer-to-members? Explain.

1078


How to declare a function pointer?

960


What is pure virtual function?

1026


What is iterator in c++?

1065


What is vector pair in c++?

1182


What is the purpose of extern storage specifier?

1109


Does c++ have foreach?

993


What do you mean by function pointer?

1055


What is command line arguments in C++? What are its uses? Where we have to use this?

1195


If I is an integer variable, which is faster ++i or i++?

1026


What relational operators if statements in c++?

1128


What is the protected keyword used for?

1065


Can turbo c++ run c program?

1042


Can a program run without main function?

1100


What is implicit conversion/coercion in c++?

1370