Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > defaulting argument to previous argument

Reply
Thread Tools

defaulting argument to previous argument

 
 
Bhushit Joshipura
Guest
Posts: n/a
 
      12-29-2003
It is illegal in g++ to default argument #6 to argument #5. I get an
error

[bhushit@x1-6-00-c0-49-b3-8a-c5 constantsum]$ g++ -g ../../surface.cpp
.../../../segments/segment.cpp
.../../../segments/geodesic-geodesic-state.cpp
.../../../segments/circular-circular-state.cpp *.cpp
ysumsphere.cpp:33: `where_00_ends' was not declared in this scope

for the code
Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
int where_00_ends, int where_0max_ends = where_00_ends ) {
....
}

Definition : Monkeying
"Being able to express default arguments in terms of explicit
arguments"

My questions:
1. By the time the compiler sees arg #6, arg #5 is already seen. Why
does it fuss?
2. I can see cases in which Monkeying prevents unwanted assumptions
about explicit arguments. Do I miss a lesson in designing?
3. I fail to see anything fundamentally wrong with Monkeying. It is
just a matter of adding some more code to prologue of a function code.
Does it conflict with some absence of specification of order of
evaluation of arguments in C++?
4.I am sure I am neither the first nor the last to need this. How to
solve the situation the best? I do not want to put this burden on the
user of the class.

Any help appreciated.
-Bhushit
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      12-29-2003

"Bhushit Joshipura" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...

> My questions:
> 1. By the time the compiler sees arg #6, arg #5 is already seen. Why
> does it fuss?


There is nothing that requires left to right evaluation of function arguments.
 
Reply With Quote
 
 
 
 
Bhushit Joshipura
Guest
Posts: n/a
 
      12-30-2003
"Ron Natalie" <(E-Mail Removed)> wrote in message news:<3ff0677e$0$31908$(E-Mail Removed) om>...
> "Bhushit Joshipura" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
>
> > My questions:
> > 1. By the time the compiler sees arg #6, arg #5 is already seen. Why
> > does it fuss?

>
> There is nothing that requires left to right evaluation of function arguments.


Still questions #2 and #4 remain.
1. What if I need such a function? Am I the only one feeling the need?
2. How can I avoid putting unnecessary restriction on the ranage of
the arguments to be defaulted?
3. Is there a design strategy that would let me avoid this trap?
4. Why should my client be bothered with "always passing last
argument"?

-Bhushit
 
Reply With Quote
 
CrayzeeWulf
Guest
Posts: n/a
 
      12-30-2003
Bhushit Joshipura wrote:


>
> Still questions #2 and #4 remain.
> 1. What if I need such a function? Am I the only one feeling the need?
> 2. How can I avoid putting unnecessary restriction on the ranage of
> the arguments to be defaulted?
> 3. Is there a design strategy that would let me avoid this trap?
> 4. Why should my client be bothered with "always passing last
> argument"?
>

You can use overloading to get full control over this process. How about
something like:

class Ysumsphere {
public:
Ysumsphere::Ysumsphere( int x_min,
int x_max,
int y_min,
int y_max,
int where_00_ends,
int where_0max_ends ) :
mXMin( x_min ),
mXMax( x_max ),
mYMin( y_min ),
mYMax( y_max ),
mWhere00Ends( where_00_ends ),
mWhere0MaxEnds( where_0max_ends ) { /* do something */ }

Ysumsphere::Ysumsphere( int x_min,
int x_max,
int y_min,
int y_max,
int where_00_ends ) :
mXMin( x_min ),
mXMax( x_max ),
mYMin( y_min ),
mYMax( y_max ),
mWhere00Ends( where_00_ends ),
mWhere0MaxEnds( where_00_ends ) { /* do something */ }
private:
int mXMin ;
int mXMax ;
int mYMin ;
int mYMax ;
int mWhere00Ends ;
int mWhere0MaxEnds ;
} ;

void yuck()
{
Ysumsphere foo( 1, 2, 3, 4, 5, 6 ) ;
Ysumsphere bar( 1, 2, 3, 4, 5 ) ;
return ;
}

--
CrayzeeWulf
 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      12-30-2003

"Bhushit Joshipura" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> It is illegal in g++ to default argument #6 to argument #5. I get an
> error
>
> [bhushit@x1-6-00-c0-49-b3-8a-c5 constantsum]$ g++ -g ../../surface.cpp
> ../../../segments/segment.cpp
> ../../../segments/geodesic-geodesic-state.cpp
> ../../../segments/circular-circular-state.cpp *.cpp
> ysumsphere.cpp:33: `where_00_ends' was not declared in this scope
>
> for the code
> Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
> int where_00_ends, int where_0max_ends = where_00_ends ) {
> ...
> }
>
> Definition : Monkeying
> "Being able to express default arguments in terms of explicit
> arguments"
>
> My questions:
> 1. By the time the compiler sees arg #6, arg #5 is already seen. Why
> does it fuss?


because the order of evaluation of arguments is implementation defined.
Further teh standard specifically prohibits this (for the above reason)

> 2. I can see cases in which Monkeying prevents unwanted assumptions
> about explicit arguments. Do I miss a lesson in designing?
> 3. I fail to see anything fundamentally wrong with Monkeying. It is
> just a matter of adding some more code to prologue of a function code.
> Does it conflict with some absence of specification of order of
> evaluation of arguments in C++?


Yes - where_00_ends is a local variable that may or may not have been
initialised to the value of the supplied parameter when it is used to init
where_0max_ends

> 4.I am sure I am neither the first nor the last to need this. How to
> solve the situation the best? I do not want to put this burden on the
> user of the class.


Simple - use overloading:

Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
int where_00_ends, int where_0max_ends) ;

Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
int where_00_ends) ;

Unfortunately the second ctor can't call the first but the that doesn't
really matter for simple classes.

If you have more complicated construction and really want the effect of ctor
calling ctor then the same effect can be acheived by pushing the core stuff
into a base class - the overloaded ctors then call call the base ctor with
all the 'default' args.

>
> Any help appreciated.
> -Bhushit



 
Reply With Quote
 
ske
Guest
Posts: n/a
 
      12-30-2003
Nick Hounsome wrote:
> "Bhushit Joshipura" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
>>It is illegal in g++ to default argument #6 to argument #5. I get an
>>error
>>
>>[bhushit@x1-6-00-c0-49-b3-8a-c5 constantsum]$ g++ -g ../../surface.cpp
>>../../../segments/segment.cpp
>>../../../segments/geodesic-geodesic-state.cpp
>>../../../segments/circular-circular-state.cpp *.cpp
>>ysumsphere.cpp:33: `where_00_ends' was not declared in this scope
>>
>>for the code
>>Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
>>int where_00_ends, int where_0max_ends = where_00_ends ) {
>>...
>>}
>>
>>Definition : Monkeying
>>"Being able to express default arguments in terms of explicit
>>arguments"
>>
>>My questions:
>>1. By the time the compiler sees arg #6, arg #5 is already seen. Why
>>does it fuss?

>
>
> because the order of evaluation of arguments is implementation defined.
> Further teh standard specifically prohibits this (for the above reason)
>
>
>>2. I can see cases in which Monkeying prevents unwanted assumptions
>>about explicit arguments. Do I miss a lesson in designing?
>>3. I fail to see anything fundamentally wrong with Monkeying. It is
>>just a matter of adding some more code to prologue of a function code.
>>Does it conflict with some absence of specification of order of
>>evaluation of arguments in C++?

>
>
> Yes - where_00_ends is a local variable that may or may not have been
> initialised to the value of the supplied parameter when it is used to init
> where_0max_ends
>
>
>>4.I am sure I am neither the first nor the last to need this. How to
>>solve the situation the best? I do not want to put this burden on the
>>user of the class.

>
>
> Simple - use overloading:
>
> Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
> int where_00_ends, int where_0max_ends) ;
>
> Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
> int where_00_ends) ;
>
> Unfortunately the second ctor can't call the first but the that doesn't
> really matter for simple classes.
>
> If you have more complicated construction and really want the effect of ctor
> calling ctor then the same effect can be acheived by pushing the core stuff
> into a base class - the overloaded ctors then call call the base ctor with
> all the 'default' args.


Alternatively if the parameters are only used to initialise member
variables then a single constructor with a member initialisation list
should do the trick (without having to duplicate constructors or add a
base class):

ie:
Ysumsphere::Ysumsphere ( int x_min, int x_max, int y_min, int y_max,
int where_00_ends) : where_0max_ends(where_00_ends) { ... }

Assuming of course that where_0max_ends is a member of the class.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defaulting FileOpenDialog to previous directory sreastman General Computer Support 1 10-23-2013 06:38 AM
function with list argument defaulting to [] - what's going on here??? Mike Python 9 04-15-2007 12:23 PM
Custom control, defaulting style properties Jeremy Chapman ASP .Net 0 03-07-2006 07:35 PM
?? dropdownlist in datagrid, not defaulting to current value ??? THANK YOU jason@cyberpine.com ASP .Net 2 02-16-2005 03:10 PM
Defaulting empty XML elements Jyrki Keisala XML 4 01-02-2004 04:01 PM



Advertisments