doug@asur.cs.cornell.edu (Douglas Campbell) (12/05/89)
I don't know whether the following is a bug or not. Help, please?
Suppose a function takes a class instance (called by value), and it is
called with a reference to an instance, which is returned from a
function. That is, the function is declared as:
fn (CoolClass i)
and called as:
fn (rcc()) // rcc returns type CoolClass&
Then shouldn't the object pointed to by rcc()'s return value be copied
into an argument for fn, using either bitwise-copy or a user-defined
X(X&) constructor?
If not, what? If so, why does this fail to compile with g++ 1.36.1 on
both Sun3 and Sun4 os4:
----------------------------------------------------------------------
#include <stream.h>
class IntClass
{
int i;
public:
IntClass() { i = 17; } // Constructor
friend IntClass& same (IntClass& ic) { return ic; }
friend void pr(IntClass ic) { cout << ic.i << "\n"; }
};
main(int argc, char *argv[])
{
IntClass a;
pr(same(a));
}
----------------------------------------------------------------------
The compilation goes:
g++ -g -c test.cc
test.cc: In function int main (int, char **):
test.cc:16: too many arguments for constructor `IntClass'
test.cc:16: in base initialization for class `IntClass'
where line 16 is: pr(same(a));
The following adjustments to the source cause the problem to disappear:
* Make pr() called by reference
* Replace line 16 with lines: IntClass& b = same(a); pr(b);
* Delete constructor for IntClass
I have this uneasy feeling that there is a trivial explanation, and
I'm going to look pretty silly. Oh well, the price of education.
Much thanks,
Doug Campbell
doug@svax.cs.cornell.edu