www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/22/13:57:34

Date: Thu, 22 May 1997 19:08:38 +0200 (MET DST)
From: Wojciech Piechowski <voyt AT delta DOT ds2 DOT pg DOT gda DOT pl>
To: Stefano Brozzi <brozzis AT mag00 DOT cedi DOT unipr DOT it>
cc: djgpp AT delorie DOT com
Subject: Re: C++ problem
In-Reply-To: <33842D98.258A@mag00.cedi.unipr.it>
Message-ID: <Pine.GSO.3.96.970522185510.19028A-100000@delta.ds2.pg.gda.pl>
MIME-Version: 1.0


On Thu, 22 May 1997, Stefano Brozzi wrote:

> in C++ is possible to have default arguments to member functions
> (i.e. int foo( int bar = 3 ) {...} ) .
> Could I have, as default value, the value of a member variable ?
> (i.e. something like:
> 
> struct Question {
>    int zoo;
>    Question() : zoo(3) {}       // this makes everybody happy ;)
>    int foo( int bar = Question::zoo );

You can write that only if zoo is static field or a global variable.

> }
> 
> )
> Directly inlined the compiler says that the data structure is not
> complete.
> If I put the function body outside the class def the compiler excuses:
> 
>         sorry, not implemented: operand of OFFSET_REF not understood
> 
> Is there a smart move-around ?
> My dumb solution is :
> 
> struct Question {
>    int zoo;
>    int foo( int bar = -1 ); // where -1 is an impossible value

You can overload foo:
     int foo( int bar );
     int foo() { foo(zoo); };

> }
> 
> int Question::foo( int bar = -1 )
Just:            ..( int bar )

> {
witout this:
>  /*       if (bar == -1) bar = zoo;  */


>                 ...
> }
> 

AFAIK, there is no way to use normal class fields as default args.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019