Using (return val of) member function as default parameter of member function

2 views
Skip to first unread message

Djm

unread,
Nov 6, 2009, 10:40:47 AM11/6/09
to
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

"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! ]

Chris Morley

unread,
Nov 6, 2009, 4:33:34 PM11/6/09
to

"Djm" <d...@jetzweb.de> wrote in message
news:c6d076bb-e4c3-4b69...@v25g2000yqk.googlegroups.com...

> 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
>
> "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();
> };

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

peter koch larsen

unread,
Nov 6, 2009, 4:34:20 PM11/6/09
to
On 6 Nov., 16:40, 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
>
> "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);
tBcPins* allPins();
void fillLevelMainFromAux(tBcPins* Pins = allPins())
{
fillLevelMainFromAux(allPins());
}

};


/Peter

marcin...@gmail.com

unread,
Nov 6, 2009, 4:34:00 PM11/6/09
to
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?

> "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

Neil Butterworth

unread,
Nov 6, 2009, 5:27:01 PM11/6/09
to
Djm 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
>
> "error: cannot call member function �tBcPins* tBcPinMan::allPins()�
> without object"

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

Bo Persson

unread,
Nov 6, 2009, 5:27:02 PM11/6/09
to
Djm 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
>
> "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();
> };

It's not a hack actually, but you can always add another overload for
the function:

void fillLevelMainFromAux()
{ fillLevelMainFromAux(allPins()); }


Bo Persson

red floyd

unread,
Nov 6, 2009, 9:03:03 PM11/6/09
to
On Nov 6, 1:34 pm, "marcin.sfi...@gmail.com" <marcin.sfi...@gmail.com>
wrote:

> 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?
>
>
>
> > "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 ;)
>

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().

Seungbeom Kim

unread,
Nov 7, 2009, 10:16:33 AM11/7/09
to

In the above example, the name of the class is tBcPinMan,
and fillLevelMainFromAux does not name a type.

--
Seungbeom Kim

Bart van Ingen Schenau

unread,
Nov 7, 2009, 10:19:32 AM11/7/09
to
marcin...@gmail.com wrote:

> 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/

Reply all
Reply to author
Forward
0 new messages