Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!jarthur!uci-ics!rfg From: rfg@ics.uci.edu (Ronald Guilmette) Newsgroups: comp.lang.c++ Subject: Re: functions as default parameters Message-ID: <25EA470D.20074@paris.ics.uci.edu> Date: 27 Feb 90 09:23:25 GMT References: <4800087@m.cs.uiuc.edu> Reply-To: rfg@ics.uci.edu (Ronald Guilmette) Organization: UC Irvine Department of ICS Lines: 28 In article <4800087@m.cs.uiuc.edu> nelson@m.cs.uiuc.edu writes: > >Well, today I discovered that default parameters can be a function, that > is to say that the following is legal: > class Fred { > public: > void bill (int b = printf ("default")) {}; > } > >Now what I would _like_ to do is: > class Jim { > public: > void printData (char *data, int length = strlen (data)) {}; > } >(That is to say that we can assume that if length is not supplied, then the > passed data is actually a string.) > >But it gets upset since "data" is not yet defined/in that scope. Is there > a way to get around this??? No. A default value expression for a formal parameter can be any kind of expression (including function calls) and it can be arbitrarily complex, but the expression has to be valid IN THE CONTEXT where it appears (just like all other expressions). // Ron Guilmette (rfg@ics.uci.edu) // C++ Entomologist // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.