[comp.lang.c++] default reference arguments

turner@s44.csrd.uiuc.edu (Steve Turner) (02/17/90)

I have a relatively simple question that I can't seem to find the
answer to in my copy of Lippman or Stroustrup, and doesn't have a
simple answer that I can think of.  To wit:

How do you declare the initializer for a default reference argument,
particularly if the default value is a member of the same class?  The
following code fragment should make this more clear.

class foo
{ public: int i; };  

class bar   // this is in file test.C
{
private:
  foo foobar;
public:  
  baz(foo &fooInstance=foobar);
};

Basically, I would like to use the local data member as the argument,
unless the routine calling bar::baz() supplies an alternative.  Sadly
AT&T 2.0 chokes on this with an error as:

CC -c test.C
CC  test.C:
"test.C", line 9: error:  object or object pointer missing for bar::foobar
1 error


Any help would be appreciated.  I think this issue fell between the
cracks in Lippman, because he discusses default arguments in section
3.5 (pg. 115) and reference arguments in section 3.7 (pg.119).
Stroustrup was no help either (as usual, for me...)

steve turner@uicsrd.csrd.uiuc.edu

rfg@ics.uci.edu (Ronald Guilmette) (02/18/90)

In article <TURNER.90Feb16210511@s44.csrd.uiuc.edu> turner@s44.csrd.uiuc.edu (Steve Turner) writes:
>I have a relatively simple question that I can't seem to find the
>answer to in my copy of Lippman or Stroustrup, and doesn't have a
>simple answer that I can think of.  To wit:
>
>How do you declare the initializer for a default reference argument,
>particularly if the default value is a member of the same class?  The
>following code fragment should make this more clear.
>
>class foo
>{ public: int i; };  
>
>class bar   // this is in file test.C
>{
>private:
>  foo foobar;
>public:  
>  baz(foo &fooInstance=foobar);
>};
>
>Basically, I would like to use the local data member as the argument,

I believe that the problem lies here.  Use of non-static class members
within the default argument expressions of member function is illegal.

Section 8.2.6 of the 2.0 Reference Manual clearly states this
restriction.

// rfg