why const member, reference member can only be initialized not assigned

Discussion in 'C++' started by ww, Oct 26, 2007.

  1. ww

    ww Guest

    I read a couple of books that tells this, but seems not one can
    explain why is this


    thanks
     
    ww, Oct 26, 2007
    #1
    1. Advertising

  2. ww wrote:
    > I read a couple of books that tells this, but seems not one can
    > explain why is this


    A const member is constant. Assignment requires a modifiable
    object. So, assignment is not an option for 'const', they need
    to be initialised. References can be "assigned to", although they
    execute the assignment so that the referred object is assigned to.
    However, in order to refer to anything, they have to be initialised
    and that's the only way to make a reference to refer to something.
    That's why const members and members that are references need to be
    initialised.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 26, 2007
    #2
    1. Advertising

  3. ww

    cplusplus Guest

    Re: why const member, reference member can only be initialized not assigned

    Hey loser why don't you do something productive with your life rather
    than sulk like a dog
     
    cplusplus, Oct 26, 2007
    #3
  4. Re: why const member, reference member can only be initialized not assigned

    On Oct 26, 1:35 am, ww <> wrote:
    > I read a couple of books that tells this, but seems not one can
    > explain why is this
    >
    > thanks


    For references, the standard says that you cannot reseat them. I guess
    that the technical reason is that when you call operator= it modifies
    the value in the reference not the reference itself - which is what
    you would expect.

    int a = 3;
    int& b(a);
    //this will change the value of b not what it refers to
    b = 5;

    Since we can't reseat a reference it has to be defined at creation
    time and hence assigned.

    Likewise for const members, we can't call the assignment operator on
    it so it must be initialised not assigned into. The const keyword
    tells the compiler that calling the assignment operator is a syntax
    error but we must have a mechanism to initialise which is done in the
    constructor.
     
    Jonathan Lane, Oct 26, 2007
    #4
  5. ww

    James Kanze Guest

    Re: why const member, reference member can only be initialized not assigned

    On Oct 26, 11:47 am, Jonathan Lane <>
    wrote:
    > On Oct 26, 1:35 am, ww <> wrote:


    > > I read a couple of books that tells this, but seems not one can
    > > explain why is this


    > For references, the standard says that you cannot reseat them. I guess
    > that the technical reason is that when you call operator= it modifies
    > the value in the reference not the reference itself - which is what
    > you would expect.


    The technical reason is that it is illegal to create a reference
    or a const object with a trivial constructor without
    initializing it. Regardless of whether they are members or not.
    If they are members, of course, the only way you can initialize
    them is in the initialization list; once you get into the body
    of the constructor, they've been initialized.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Oct 26, 2007
    #5
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,009
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    14
    Views:
    698
    Kai-Uwe Bux
    Sep 18, 2006
  3. Javier
    Replies:
    2
    Views:
    572
    James Kanze
    Sep 4, 2007
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. , India
    Replies:
    5
    Views:
    1,784
    chisholm
    Feb 15, 2011
Loading...

Share This Page