By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,136 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,136 IT Pros & Developers. It's quick & easy.

default struct argument, how?

P: n/a
Hi there.

I want to use a default argument declared as a struct, something like:

struct mystruct { int a; int b; };
void myfunc ( mystruct x = {0, 1} )
{
// Do something
}

But my compiler (Visual Studio 2005) just complains. So, what is wrong with
the above?

Thanks in advance
Pär
Dec 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Pär Buschka wrote:
Hi there.

I want to use a default argument declared as a struct, something like:
structure with default values or ......
struct mystruct { int a; int b; };
void myfunc ( mystruct x = {0, 1} )
{
// Do something
}

But my compiler (Visual Studio 2005) just complains. So, what is wrong with
the above?

Thanks in advance
Pär
Could you tell the purpose of myfunc() function???

Dec 19 '06 #2

P: n/a
Pär Buschka wrote:
Hi there.

I want to use a default argument declared as a struct, something like:

struct mystruct { int a; int b; };
void myfunc ( mystruct x = {0, 1} )
{
// Do something
}

But my compiler (Visual Studio 2005) just complains. So, what is wrong
with the above?
Well, it's invalid syntax. What you can do is give it a constructor:

struct mystruct
{
mystruct(int a, int b) : a(a), b(b) {}
int a; int b;
};

void myfunc ( mystruct x = mystruct(0, 1) )
{
// Do something
}
Dec 19 '06 #3

P: n/a
"Pär Buschka" <pe**@home.sewrote in message
news:CV*******************@newsb.telia.net
Hi there.

I want to use a default argument declared as a struct, something like:

struct mystruct { int a; int b; };
void myfunc ( mystruct x = {0, 1} )
{
// Do something
}

But my compiler (Visual Studio 2005) just complains. So, what is
wrong with the above?

Thanks in advance
Pär
An alternative to Rolf's suggestion is:

struct mystruct { int a; int b; };
mystruct initializer = {0,1};
void myfunc ( mystruct x = initializer)
{
// Do something
}
--
John Carson
Dec 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.