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: <25EA5503.23178@paris.ics.uci.edu> Date: 27 Feb 90 10:23:00 GMT References: <4800087@m.cs.uiuc.edu> <25EA470D.20074@paris.ics.uci.edu> Reply-To: rfg@ics.uci.edu (Ronald Guilmette) Organization: UC Irvine Department of ICS Lines: 64 In article <25EA470D.20074@paris.ics.uci.edu> rfg@ics.uci.edu (Ronald Guilmette) writes: >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). Sorry!!! I didn't realize during my earlier posting that `data' was also declared in that same formal parameter list. This is more complex that I thought! The problem here is that C++ trashed a perfectly good concept from ANSI C, i.e. function-prototype scopes. Such a concept is not officially supported in C++, and that's a pity. It make the actual rules harder to figure out and more deviant from C. Note that this is legal: int foo () { int x = x; } While cfront 2.0 rejects the following: int bar (int x = x) { } I'm not at all sure why it rejects it, and I do not believe that the manual clarifies exactly why this should be rejected. Obviously, this is one sense in which C++ *does* believe in the concept of prototype scope! In particular it seems that some special rules must be stated that only apply to prototype regions, e.g. If an object is declared in block scope then its name is visible immediately after the end of its full declarator, however if an object is declared in function prototype scope, then it cannot be redeclared in that same scope, and it is not visible until the start of the following block. // Ron Guilmette (rfg@ics.uci.edu) // C++ Entomologist // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.