"santosh" <> wrote in message
news:d6sqog$1f7$...
> Hello,
> I have const member in the class.
> How can I initialise these.
Do it the same way any member is initialized,
in the initializer list.
> I can not initialise in constructor ,(it is giving compilation error)
Nothing can be initialized in the constructor body,
as when it begins to execute it's too late. What
you have in your code below is not initialization,
it's assignment, not the same thing.
> What is the proper way to initialise.
Initializer list. See below.
> The code is given below.
> Regards,
> santosh
>
>
>
>
> class NamedPtr
> {
> const string a;
> const int i;
> const int* ptr_int;
> public:
> NamedPtr(const string &s,int * x,int b=90)
> {
> cout<<"constor"<<endl;
> //=b;
> //s;
> //r_int=x;
> }
NamedPtr(const string& s, int *x, int b = 90)
: a(s), ptr_int(x), i(b) /* initializer list */
{
}
-Mike
|