Can a class be used in default arguments?

C

Cruxic

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
 
A

Alf P. Steinbach

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!
};
....

So, is this type of default argument non-standard?

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

Kevin Goodsell

Cruxic said:
Can a class constructor be an argument default?

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.
void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
HERE!

....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
 
S

Siana

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.


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

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

Kevin Goodsell

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

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

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top