Click here to Skip to main content
Click here to Skip to main content

How to implement a default value for a struct parameter

, 11 Dec 2003
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Mr Scotty
Architect
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
GeneralPerformance issue PinmemberRoman Keskenti [SmoCoder]14-Dec-03 9:13 
QuestionHow about that PinmemberFranz Brunner12-Dec-03 4:58 
AnswerRe: How about that PinmemberMr Scotty12-Dec-03 5:07 
GeneralRe: How about that PinmemberJohn M. Drescher12-Dec-03 5:13 
GeneralRe: How about that PinmemberJohn Carson13-Dec-03 14:50 
AnswerRe: How about that PinmemberJohn M. Drescher12-Dec-03 5:11 
AnswerRe: How about that PinmemberRabidCow13-Dec-03 6:50 
QuestionWrong section? Pinmemberdog_spawn12-Dec-03 4:48 
AnswerRe: Wrong section? PinmemberPaolo Messina12-Dec-03 7:33 
AnswerRe: Wrong section? PinmemberJörgen Sigvardsson12-Dec-03 10:39 
GeneralRe: Wrong section? Pinmemberdog_spawn12-Dec-03 13:11 
GeneralRe: Wrong section? PinmemberJörgen Sigvardsson12-Dec-03 13:18 
GeneralWhat does the 'a' stand for? Pinmemberdog_spawn12-Dec-03 15:31 
GeneralRe: What does the 'a' stand for? PinmemberJohn Carson13-Dec-03 15:57 
GeneralMisquoteathon Pinmemberdog_spawn14-Dec-03 4:15 
GeneralRe: Misquoteathon PinmemberJohn Carson14-Dec-03 4:43 
GeneralRe: Misquoteathon Pinmembervertigo00oo15-Dec-03 3:35 
AnswerRe: Wrong section? PinmemberPeter Ritchie15-Dec-03 7:46 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 12 Dec 2003
Article Copyright 2003 by Mr Scotty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid