Click here to Skip to main content
15,885,895 members
Articles / Programming Languages / C++
Article

How to implement a default value for a struct parameter

Rate me:
Please Sign up or sign in to vote.
2.47/5 (34 votes)
11 Dec 2003 140K   16   18
How to implement a default value for a struct parameter.

Introduction

Someone asked me how to implement a default parameter value for a struct. He wanted to make it possible to make the following calls:

MyStruct st = {200,50);

foo();   // No parameters means the default ones
foo(st); // Use the parameters passed

He had tried the following:

struct MyStruct 
{ 
    int a; 
    int b;

};
 
void foo(MyStruct st={100,100}) 
{
    cout << st.a << " " << st.b << endl;
}

The compiler (VC 6) however didn't compile this.

Solutions

You can use a constructor in your struct. It's not really nice but it will work.

struct MyStruct 
{
    MyStruct(int x, int y)
    {
        a = x;
        b = y;
    }
    int a; 
    int b;

};

void foo(MyStruct st=MyStruct(100,100)) 
{
    cout << st.a << " " << st.b << endl; 
}

The second solution, I like a little more:

struct MyStruct 
{
    int a; 
    int b;
};
static const MyStruct stDefValue = { 100, 100 };

void foo(MyStruct st=stDefValue) 
{
    cout << st.a << " " << st.b << endl;
}

Conclusion

It's not a big deal and you may find it not nicely programmed in an OO way, but it is handy to know when you want to do something like this.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPerformance issue Pin
Roman Keskenti [SmoCoder]14-Dec-03 9:13
Roman Keskenti [SmoCoder]14-Dec-03 9:13 
QuestionHow about that Pin
Franz Brunner12-Dec-03 4:58
Franz Brunner12-Dec-03 4:58 
AnswerRe: How about that Pin
Mr Scotty12-Dec-03 5:07
Mr Scotty12-Dec-03 5:07 
With the default parameter you can do

foo();

and the parameters passed to foo will have the default.
GeneralRe: How about that Pin
John M. Drescher12-Dec-03 5:13
John M. Drescher12-Dec-03 5:13 
GeneralRe: How about that Pin
John Carson13-Dec-03 14:50
John Carson13-Dec-03 14:50 
AnswerRe: How about that Pin
John M. Drescher12-Dec-03 5:11
John M. Drescher12-Dec-03 5:11 
AnswerRe: How about that Pin
RabidCow13-Dec-03 6:50
RabidCow13-Dec-03 6:50 
QuestionWrong section? Pin
dog_spawn12-Dec-03 4:48
dog_spawn12-Dec-03 4:48 
AnswerRe: Wrong section? Pin
Paolo Messina12-Dec-03 7:33
professionalPaolo Messina12-Dec-03 7:33 
AnswerRe: Wrong section? Pin
Jörgen Sigvardsson12-Dec-03 10:39
Jörgen Sigvardsson12-Dec-03 10:39 
GeneralRe: Wrong section? Pin
dog_spawn12-Dec-03 13:11
dog_spawn12-Dec-03 13:11 
GeneralRe: Wrong section? Pin
Jörgen Sigvardsson12-Dec-03 13:18
Jörgen Sigvardsson12-Dec-03 13:18 
GeneralWhat does the 'a' stand for? Pin
dog_spawn12-Dec-03 15:31
dog_spawn12-Dec-03 15:31 
GeneralRe: What does the 'a' stand for? Pin
John Carson13-Dec-03 15:57
John Carson13-Dec-03 15:57 
GeneralMisquoteathon Pin
dog_spawn14-Dec-03 4:15
dog_spawn14-Dec-03 4:15 
GeneralRe: Misquoteathon Pin
John Carson14-Dec-03 4:43
John Carson14-Dec-03 4:43 
GeneralRe: Misquoteathon Pin
vertigo00oo15-Dec-03 3:35
vertigo00oo15-Dec-03 3:35 
AnswerRe: Wrong section? Pin
Peter Ritchie15-Dec-03 7:46
Peter Ritchie15-Dec-03 7:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.