why const member, reference member can only be initialized not assigned - c++

This is a discussion on why const member, reference member can only be initialized not assigned - c++ ; I read a couple of books that tells this, but seems not one can explain why is this thanks...

+ Reply to Thread
Results 1 to 5 of 5

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

  1. Default why const member, reference member can only be initialized not assigned

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


    thanks


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

    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



  3. Default 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


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

    On Oct 26, 1:35 am, ww <mrw...@gmail.com> 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.


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

    On Oct 26, 11:47 am, Jonathan Lane <jonathan.la...@googlemail.com>
    wrote:
    > On Oct 26, 1:35 am, ww <mrw...@gmail.com> 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:james.kanze@gmail.com
    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


+ Reply to Thread

Similar Threads

  1. Passing a static const class member as reference
    By Application Development in forum c++
    Replies: 6
    Last Post: 11-28-2007, 12:56 PM
  2. static member function and const
    By Application Development in forum c++
    Replies: 8
    Last Post: 11-15-2007, 12:06 PM
  3. Initialization of a const POD member
    By Application Development in forum c++
    Replies: 18
    Last Post: 10-24-2007, 01:27 PM
  4. Disambiguating between const and non-const member
    By Application Development in forum c++
    Replies: 2
    Last Post: 07-04-2007, 02:02 PM
  5. Const member function
    By Application Development in forum c++
    Replies: 14
    Last Post: 06-16-2007, 02:55 PM