Is it not possible to use (the return value of one member function)
as default value for a parameter in another member function of the
same class? For example In the code below I was suprised to get the
compiler error
"error: cannot call member function �tBcPins* tBcPinMan::allPins()�
without object"
If its not possible is there any decent "hack" or alternative to do
achieve the same.
Thanks very much in advance
class tBcPinMan
{
......
void fillLevelMainFromAux(tBcPins * Pins = allPins());
tBcPins * allPins();
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
I don't know about using the return value of a member function but there is
a simple work around if no one else knows or it is imposible. You can try
using a 'magic' pointer value (e.g. NULL) to indicate you want default
behaviour then top line of your fn, test the argument against this value &
call the initialiser if necessary.
e.g.
class tBcPinMan {
void fillLevelMainFromAux(tBcPins * Pins = 0);
};
void tBcPinMan::fillLevelMainFromAux(tBcPins * Pins)
{
if (Pins==0) Pins=allPins();
...
}
Chris
class tBcPinMan
{
......
void fillLevelMainFromAux(tBcPins* Pins);
tBcPins* allPins();
void fillLevelMainFromAux(tBcPins* Pins = allPins())
{
fillLevelMainFromAux(allPins());
}
};
/Peter
Default value for a parameter can only be a compile-time constant. Or
am I wrong?
> "error: cannot call member function �tBcPins* tBcPinMan::allPins()�
> without object"
>
> If its not possible is there any decent "hack" or alternative to do
> achieve the same.
>
> Thanks very much in advance
>
> class tBcPinMan
> {
> ......
> void fillLevelMainFromAux(tBcPins * Pins = allPins());
> tBcPins * allPins();
>
> };
>
class tBcPinMan {
void fillLevelMainFromAux(tBcPins* Pins);
void fillLevelMainFromAux() {
fillLevelMainFromAux(allPins());
}
tBcPins* allPins();
};
Rather decent piece of code I would say ;)
Cheers
Sfider
It's not very suprising - there is no object to call the function on.
The "this" pointer is on;y available in the body of the function.
> If its not possible is there any decent "hack" or alternative to do
> achieve the same.
Make the allPins() function static.
Neil Butterworth
It's not a hack actually, but you can always add another overload for
the function:
void fillLevelMainFromAux()
{ fillLevelMainFromAux(allPins()); }
Bo Persson
Not really. Maybe in C++0x, but in C++03, it doesn't do what you
think it does.
Your default constructor does nothing, except declare a local (to the
constructor) anonymous temp variable of type fillLevelMainFromAux().
In the above example, the name of the class is tBcPinMan,
and fillLevelMainFromAux does not name a type.
--
Seungbeom Kim
> On Nov 6, 4:40 pm, Djm <d...@jetzweb.de> wrote:
>> Hello.
>>
>> Is it not possible to use (the return value of one member function)
>> as default value for a parameter in another member function of the
>> same class? For example In the code below I was suprised to get the
>> compiler error
>
> Default value for a parameter can only be a compile-time constant. Or
> am I wrong?
You are wrong.
The expression used for a default value has to fulfil two special
criteria:
1. All names must be looked up at the point where the default argument
appears in the code
2. It must be possible to evaluate the expression in the context of the
caller of the function.
It is that last requirement that makes using a non-static member-
function nearly impossible for default arguments.
>
> Cheers
> Sfider
>
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/