Mail Archives: djgpp/1997/05/22/13:57:34
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 -