const references : confusion

Discussion in 'C++' started by raj, Feb 12, 2004.

  1. raj

    raj Guest

    I need explanation of the behavior of the following code. When run as it is, it gives error.

    //-------------------------<start>------------------
    #include <iostream.h>
    #include <stdlib.h>

    class ABC
    {
    private: int x;

    public:
    ABC() { x=0; }
    ABC (int a) { x=a; }
    ABC (const ABC& abc)
    {
    x=abc.x+1; //increasin by 1 just as a token of copy constructor invocation
    cout<<"\n\tCopy constructor invoked.";
    }

    ABC& operator=(ABC& abc) //NO ERROR IF CHANGED TO : ABC& operator=(const ABC& abc)
    { //line 18
    x=abc.x;
    return (*this);
    }
    ABC friend frnd_func();
    };

    ABC frnd_func()
    {
    ABC abc(100);
    return abc;
    }

    int main()
    {
    ABC a1(5);
    ABC a2=a1; //just to test copy constructor.
    a2=frnd_func(); //gives error : "initialisation of non-const reference type 'cxlass ABC&'
    // from rvalue of typpe 'ABC' in passing argument 1 of 'ABC::eek:perator=(ABC&)'
    cout<<"\n\n";system("PAUSE");
    return 0;
    }
    //-------------------------<end>---------------------

    Running it in Bloodshed Dev C++ gives the following error:
    35: Initialisation of non-const reference type 'class ABC&'
    35: from rvalue of type 'ABC'
    18: in assigning argument 1 of 'ABC::eek:perator=(ABC &)'

    But, if I change the = overloading declaration to:
    ABC& operator=(const ABC& abc)
    then the error vanishes.

    Why should i need to add the 'const' keyword here?
    raj, Feb 12, 2004
    #1
    1. Advertising

  2. raj

    ququqa Guest

    In expression:
    a2=frnd_func();
    frnd_funct() creates a temporary object ABC that is assigned to a2.
    All temporary objects are const (you cannot change them), so the
    operator: ABC& operator=(ABC& abc) has to take a const reference to
    assign temporary object.

    regareds
    q

    http://members.lycos.co.uk/ququqa2/

    raj wrote:

    > I need explanation of the behavior of the following code. When run as it is, it gives error.
    >
    > //-------------------------<start>------------------
    > #include <iostream.h>
    > #include <stdlib.h>
    >
    > class ABC
    > {
    > private: int x;
    >
    > public:
    > ABC() { x=0; }
    > ABC (int a) { x=a; }
    > ABC (const ABC& abc)
    > {
    > x=abc.x+1; //increasin by 1 just as a token of copy constructor invocation
    > cout<<"\n\tCopy constructor invoked.";
    > }
    >
    > ABC& operator=(ABC& abc) //NO ERROR IF CHANGED TO : ABC& operator=(const ABC& abc)
    > { //line 18
    > x=abc.x;
    > return (*this);
    > }
    > ABC friend frnd_func();
    > };
    >
    > ABC frnd_func()
    > {
    > ABC abc(100);
    > return abc;
    > }
    >
    > int main()
    > {
    > ABC a1(5);
    > ABC a2=a1; //just to test copy constructor.
    > a2=frnd_func(); //gives error : "initialisation of non-const reference type 'cxlass ABC&'
    > // from rvalue of typpe 'ABC' in passing argument 1 of 'ABC::eek:perator=(ABC&)'
    > cout<<"\n\n";system("PAUSE");
    > return 0;
    > }
    > //-------------------------<end>---------------------
    >
    > Running it in Bloodshed Dev C++ gives the following error:
    > 35: Initialisation of non-const reference type 'class ABC&'
    > 35: from rvalue of type 'ABC'
    > 18: in assigning argument 1 of 'ABC::eek:perator=(ABC &)'
    >
    > But, if I change the = overloading declaration to:
    > ABC& operator=(const ABC& abc)
    > then the error vanishes.
    >
    > Why should i need to add the 'const' keyword here?
    >
    ququqa, Feb 12, 2004
    #2
    1. Advertising

  3. "ququqa" <> wrote in message
    news:c0gs7d$2bi1$...
    > In expression:
    > a2=frnd_func();
    > frnd_funct() creates a temporary object ABC that is assigned to a2.
    > All temporary objects are const (you cannot change them),


    That is not true, for instance

    X func();

    X x;
    func() = x;

    is perfectly legal code (assuming X is a class, and assignment of X is
    legal). The return value from func is a temporary, but it is not const.

    The correct rule is that you cannot bind a non-const reference to a
    temporary object.

    This subject gets a lot of coverage in this group because it seems a lot of
    people are surprised by this rule.

    john
    John Harrison, Feb 12, 2004
    #3
  4. >
    > Running it in Bloodshed Dev C++ gives the following error:
    > 35: Initialisation of non-const reference type 'class ABC&'
    > 35: from rvalue of type 'ABC'
    > 18: in assigning argument 1 of 'ABC::eek:perator=(ABC &)'
    >
    > But, if I change the = overloading declaration to:
    > ABC& operator=(const ABC& abc)
    > then the error vanishes.
    >
    > Why should i need to add the 'const' keyword here?
    >


    Rules of C++, you cannot bind a temporary object to a non-const reference.
    The return value of a function is a temporary.

    john
    John Harrison, Feb 12, 2004
    #4
  5. raj

    Sharad Kala Guest

    "John Harrison" <> wrote in message
    news:c0gt88$17gmcs$-berlin.de...
    > >
    > > Running it in Bloodshed Dev C++ gives the following error:
    > > 35: Initialisation of non-const reference type 'class ABC&'
    > > 35: from rvalue of type 'ABC'
    > > 18: in assigning argument 1 of 'ABC::eek:perator=(ABC &)'
    > >
    > > But, if I change the = overloading declaration to:
    > > ABC& operator=(const ABC& abc)
    > > then the error vanishes.
    > >
    > > Why should i need to add the 'const' keyword here?
    > >

    >
    > Rules of C++, you cannot bind a temporary object to a non-const reference.
    > The return value of a function is a temporary.
    >

    True and it's good that your compiler has pointed it out to you.
    There are compilers (with options) which as a language extension allow binding
    temporary objects
    to non-const references. So if such an option is set/unset you may just write
    non-standard code without even realizing it.
    E.g. On VC++ compile without /Za compiler option your code would run just fine.

    -Sharad
    Sharad Kala, Feb 13, 2004
    #5
  6. raj

    raj Guest

    Thanks a lot ... that clears up the matter (and some others too).
    :)
    raj, Feb 13, 2004
    #6
  7. raj

    raj Guest

    (tho i dont undestand why the compiler shd take such pains to ensure the constness of temporary objects... particularly, whn i'm takin control of them using copy constructors)
    raj, Feb 13, 2004
    #7
  8. "raj" <> wrote in message
    news:...
    > (tho i dont undestand why the compiler shd take such pains to ensure the

    constness of temporary objects... particularly, whn i'm takin control of
    them using copy constructors)
    >


    Jesus, how many times does this have to be said.

    Temporary objects are not const, the compiler does not enforce the constness
    of temporary objects.

    There is a recent thread 'non-const function return values: gcc bug or
    language flaw' which goes into this in great detail and concludes with the
    real reason for this rule, contributed by the great man himself (Bjarne
    Stroustrup).

    john
    John Harrison, Feb 14, 2004
    #8
    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. Andrew R. Thomas-Cramer
    Replies:
    8
    Views:
    530
    Roedy Green
    Jul 11, 2003
  2. Replies:
    11
    Views:
    1,099
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,111
    Tim Rentsch
    Nov 10, 2008
  5. K. Frank
    Replies:
    4
    Views:
    179
Loading...

Share This Page