Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member const initialisation

Reply
Thread Tools

member const initialisation

 
 
santosh
Guest
Posts: n/a
 
      05-23-2005
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;
}


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      05-23-2005
"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


 
Reply With Quote
 
 
 
 
John Dibling
Guest
Posts: n/a
 
      05-23-2005
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>

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      05-23-2005
"santosh" <> wrote in message news:d6sqog$1f7$...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
Initialisation of reference vs. initialisation of reference member Tim Clacy C++ 8 05-30-2006 06:14 PM
Complex const initialisation and Intel compiler remark Ryan Mitchley C++ 0 07-19-2004 08:37 AM



Advertisments