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

Describe the advantages of operator overloading?

1048


What are the operators in c++?

1119


What are guid? Why does com need guids?

1055


What do you understand by pure virtual function? Write about its use?

1006


What is guard code in c++?

1171


Explain what is oop?

1094


What is the use of namespace std in C++?

1072


Is nan a c++?

1155


What is class definition in c++ ?

1099


What is c strings syntax?

1147


What is iterator c++?

992


Can I learn c++ without c?

1145


What is the difference between an array and a list?

1033


How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?

2316


What is ifstream c++?

1030