member const initialisation

Discussion in 'C++' started by santosh, May 23, 2005.

  1. santosh

    santosh Guest

    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;
    }
     
    santosh, May 23, 2005
    #1
    1. Advertising

  2. santosh

    Mike Wahler Guest

    "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
     
    Mike Wahler, May 23, 2005
    #2
    1. Advertising

  3. santosh

    John Dibling Guest

    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>
     
    John Dibling, May 23, 2005
    #3
  4. santosh

    Lionel B Guest

    "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
     
    Lionel B, May 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Mitchley
    Replies:
    0
    Views:
    416
    Ryan Mitchley
    Jul 19, 2004
  2. Tim Clacy
    Replies:
    8
    Views:
    406
    Tim Clacy
    May 30, 2006
  3. Replies:
    11
    Views:
    1,125
  4. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  5. fungus
    Replies:
    13
    Views:
    902
    fungus
    Oct 31, 2008
Loading...

Share This Page