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 is a lambda function c++?

1193


How can you quickly find the number of elements stored in a static array?

1137


Is it possible to write a c++ template to check for a function's existence?

1075


What is a buffer c++?

1106


What is setiosflags c++?

1026


Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.

1380


What are the uses of typedef in a program?

1132


What is meant by a delegate?

1142


What is operators in c++?

1054


Explain the static member function.

1226


What is the difference between cin.read() and cin.getline()?

1130


What is a unnitialised pointer?

1138


Can notepad ++ run c++?

1149


What is c++ programming language?

1152


Write a Program for find and replace a character in a string.

1131