Topic: conversion of class to class reference


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/09/15
Raw View
Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> writes:

> The expression `MyClass()' is an rvalue, and non-const references
> can only be initialized with lvalues.  See 8.5.3 [dcl.init.ref] paragraph 8.

Was 'rvalue' vs 'result of an implicit conversion' considered
when introducing this rule ?

In particular:
    istream (something) << complex_float(4);
would work (where complex_float(4) stands for any user-defined
type).

It would also allow other things which may not be desirable:

void    foo (int& i)
{
    i = 4;
}

foo (int ());

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: mbresnahan1@mmm.com
Date: 1997/09/12
Raw View

You could also declare foo as:

    void foo( const MyClass &, int);

which is probably what you want, looking at your code below.

BTW, the old HP cfront compiler displays an anachronism warning instead
of an error.

You're trying to pass a temporary object to a non-const reference
argrument.  If the object is modified, the changes are lost because the
object will be destructed after the function returns.  If you don't
intend to modify the object you should declare the argument as const.

BTW, does anyone know why it is considered an error now, rather than a
warning?

MikeB

----
Brett wrote:
>
> Ok, I found out that doing this in the calling function eliminates the
> problem:
>
> int bar(){
>         int x = 0;
>         MyClass tempMyClass();
>         foo(tempMyClass, x);
>         ........
> }


--
Michael J. Bresnahan, York & Associates, (612)-831-0077 (York),
(612)-737-2486 (3M), mailto:mbresnah@yorkconsulting.com,
mailto:mbresnahan1@mmm.com, http://www.visi.com/~mbresnah,
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: brett.ger-ry@uni-sys.com (Brett)
Date: 1997/09/09
Raw View
Sometimes the compiler I'm using (HP aCC ver 1.00) complains about conversion
from a class to a class reference (whereas I did not get any error messages
before).  Here is an example of my problem.

class MyClass{
        public:
                MyClass() {....}
                friend void foo(MyClass &, int i);
        ..........
}

int bar(){
        int x = 0;
        foo(MyClass(), x);
        ........
}

The compiler gives me the following error:

Error 225: "file.cxx", line xxx # "No appropriate function found for call of
'foo'.  Last viable candidate was "void foo(MyClass &, int)"
[file2.h, line yyy] Argument of type 'class MyClass' could not be converted to
'MyClass &'.

I couldn't find anything in the ANSI/ISO draft standard that would cause this.
Am I overlooking something?


-Brett

**********************************************************
* For e-mail replies omit all dashes from the            *
* address in the "From: " field.                         *
**********************************************************
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: brett.ger-ry@uni-sys.com (Brett)
Date: 1997/09/10
Raw View
Ok, I found out that doing this in the calling function eliminates the
problem:

int bar(){
        int x = 0;
        MyClass tempMyClass();
        foo(tempMyClass, x);
        ........
}

Is there a convention in the standard that forbids passing the result of a
constructor as an instance of a class?  (Note that this is a common practice
in Java.)

In article <5v1nss$5mp$1@mail.pl.unisys.com>, brett.ger-ry@uni-sys.com (Brett)
wrote:
>Sometimes the compiler I'm using (HP aCC ver 1.00) complains about conversion
>from a class to a class reference (whereas I did not get any error messages
>before).  Here is an example of my problem.
>
>class MyClass{
>        public:
>                MyClass() {....}
>                friend void foo(MyClass &, int i);
>        ..........
>}
>
>int bar(){
>        int x = 0;
>        foo(MyClass(), x);
>        ........
>}
>
>The compiler gives me the following error:
>
>Error 225: "file.cxx", line xxx # "No appropriate function found for call of
>'foo'.  Last viable candidate was "void foo(MyClass &, int)"
>[file2.h, line yyy] Argument of type 'class MyClass' could not be converted to
>'MyClass &'.
>
>I couldn't find anything in the ANSI/ISO draft standard that would cause this.
>Am I overlooking something?
>
>


-Brett

**********************************************************
* For e-mail replies omit all dashes from the            *
* address in the "From: " field.                         *
**********************************************************
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/09/10
Raw View
brett.ger-ry@uni-sys.com (Brett) writes:

>Sometimes the compiler I'm using (HP aCC ver 1.00) complains about conversion
>from a class to a class reference (whereas I did not get any error messages
>before).

The compiler is correct; this is an area in which the language has changed,
but the change was made years ago.

> Here is an example of my problem.
>
>class MyClass{
>        public:
>                MyClass() {....}
>                friend void foo(MyClass &, int i);
>        ..........
>}
>
>int bar(){
>        int x = 0;
>        foo(MyClass(), x);
>        ........
>}

The expression `MyClass()' is an rvalue, and non-const references
can only be initialized with lvalues.  See 8.5.3 [dcl.init.ref] paragraph 8.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]