By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,972 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,972 IT Pros & Developers. It's quick & easy.

member const initialisation

P: n/a
Hello,
I have const member in the class.
How can I initialise these.
I can not initialise in constructor ,(it is giving compilation error)
What is the proper way to initialise.
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;
}
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"santosh" <sa*************@in.bosch.com> wrote in message
news:d6**********@ns2.fe.internet.bosch.com...
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
Jul 23 '05 #2

P: n/a
Use an intiialization list:

NamedPtr(const string &s,int * x,int b=90)
: a(s), ptr_int(x), i(b)
{
cout<<"constor"<<endl;
}

Take care,
</dib>

Jul 23 '05 #3

P: n/a
"santosh" <sa*************@in.bosch.com> wrote in message news:d6**********@ns2.fe.internet.bosch.com...
Hello,
I have const member in the class.
How can I initialise these.
I can not initialise in constructor ,(it is giving compilation error)
What is the proper way to initialise.
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), i(b), ptr_int(x),
{
cout<<"constor"<<endl;
...
}

--
Lionel B

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.