🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Douglas Campbell Mon, 04 Dec 1989 21:28
Is this a bug?
#673
Author: Douglas Campbell
Date: Mon, 04 Dec 1989 21:28
56 lines
2201 bytes
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads