Connecting Tech Pros Worldwide Forums | Help | Site Map

Can a class be used in default arguments?

Cruxic
Guest
 
Posts: n/a
#1: Jul 19 '05
Can a class constructor be an argument default? It works in Borland
C++ but not in MinGW

The following won't compile in MinGW because of the error "Invalid
type 'Thing1' for default"
===========================
#include <stdio.h>
#include <stdlib.h>

class Thing1

{
public:
int mInt;
Thing1() {mInt = 123456789;}
};

class Thing2
{
public:
int mInt;
public:
void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
HERE!
};

int main(int argc, char *argv[])
{
Thing2 t2;
t2.setInt();
printf("%d\n", t2.mInt);
system("PAUSE");
return 0;
}
==========================
So, is this type of default argument non-standard?

Much Thanks,
Adam

Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Can a class be used in default arguments?


On 3 Oct 2003 00:14:05 -0700, cruxic@softhome.net (Cruxic) wrote:
[color=blue]
>Can a class constructor be an argument default? It works in Borland
>C++ but not in MinGW
>
>The following won't compile in MinGW because of the error "Invalid
>type 'Thing1' for default"
>===========================
>#include <stdio.h>
>#include <stdlib.h>
>
>class Thing1
>
>{
>public:
> int mInt;
> Thing1() {mInt = 123456789;}
>};
>
>class Thing2
>{
>public:
> int mInt;
>public:
> void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
>HERE!
>};
>[/color]
....
[color=blue]
>So, is this type of default argument non-standard?[/color]

Yes. But you can make that a reference to const object and it will
then be standard-conforming.

Kevin Goodsell
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Can a class be used in default arguments?


Cruxic wrote:
[color=blue]
> Can a class constructor be an argument default?[/color]

As you stated the question, the answer is 'no'. You can't ever use a
function as an argument (default or otherwise). You can use a function
pointer, but you can't get a pointer to a constructor.

But, this is not what you meant. You wanted to ask whether you can use a
temporary object as a default argument. And you can.
[color=blue]
> void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
> HERE![/color]

....however, you cannot bind a temporary to a non-const reference. That's
the problem with this code. If the reference were const, this would be fine.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Siana
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Can a class be used in default arguments?


On Fri, 03 Oct 2003 17:59:38 GMT, Kevin Goodsell
<usenet1.spamfree.fusion@neverbox.com> wrote:
[color=blue]
>Cruxic wrote:
>[color=green]
>> Can a class constructor be an argument default?[/color]
>
>As you stated the question, the answer is 'no'. You can't ever use a
>function as an argument (default or otherwise). You can use a function
>pointer, but you can't get a pointer to a constructor.
>
>But, this is not what you meant. You wanted to ask whether you can use a
>temporary object as a default argument. And you can.
>[color=green]
>> void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
>> HERE![/color]
>
>...however, you cannot bind a temporary to a non-const reference. That's
>the problem with this code. If the reference were const, this would be fine.
>
>-Kevin[/color]

Works fine if it's not a reference.
void setInt(Thing1 t = Thing1())
{
mInt = t.mInt;
}

Kevin Goodsell
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Can a class be used in default arguments?


Siana wrote:
[color=blue]
> On Fri, 03 Oct 2003 17:59:38 GMT, Kevin Goodsell
> <usenet1.spamfree.fusion@neverbox.com> wrote:
>[color=green]
>>
>>...however, you cannot bind a temporary to a non-const reference. That's
>>the problem with this code. If the reference were const, this would be fine.
>>
>>-Kevin[/color]
>
>
> Works fine if it's not a reference.
> void setInt(Thing1 t = Thing1())
> {
> mInt = t.mInt;
> }
>[/color]

Assuming Thing1 has an appropriate, accessible copy constructor, then
yes that works.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Cruxic
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Can a class be used in default arguments?


Great! Thanks for the help folks.
Closed Thread