* JustSomeGuy:
Hi I have a function that takes a refrence to a std::list<myType> &
for ex:
myMethod(int a=0, int b=0, std::list<myType> & lst=NULL)
Now most of the time the lst is not passed and so I would like to have
it given a null parameter...
How do I do this?
Almost as you did, except NULL is not an object of the right type.
However, it's generally bad design to have an optional out (or in/out)
parameter represented as a reference argument.
Instead, how about representing that as the function result?
Let's say 'myMethod' is really 'fixThings', and the troublesome argument
is a list of things fixed.
Then, how about
struct IRecorder
{
virtual void add( MyType const& ) = 0;
};
void fixThings( IRecorder& recorder, int a = 0, int b = 0 )
{
...
};
void fixThings( int a = 0, int b = 0 )
{
struct DummyRecorder: IRecorder { void add( MyType const& ) {} };
DummyRecorder r;
fixThings( r, a, b );
}
std::list<MyType> fixThingsWithRecording( int a = 0, int b = 0 )
{
struct Recorder: IRecorder
{
std::list<MyType> records;
void add( MyType const& o ){ records.push_back( o ); }
};
Recorder r;
fixThings( r, a, b );
return r.records;
}
Or something -- it depends very much on the actual problem.