Click here to Skip to main content
12,063,645 members (71,512 online)
Click here to Skip to main content
Add your own
alternative version

Stats

103.2K views
16 bookmarked
Posted

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

Share

About the Author

Mr Scotty
Architect
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralPerformance issue Pin
Roman Keskenti [SmoCoder]14-Dec-03 10:13
memberRoman Keskenti [SmoCoder]14-Dec-03 10:13 
QuestionHow about that Pin
Franz Brunner12-Dec-03 5:58
memberFranz Brunner12-Dec-03 5:58 
AnswerRe: How about that Pin
Mr Scotty12-Dec-03 6:07
memberMr Scotty12-Dec-03 6:07 
GeneralRe: How about that Pin
John M. Drescher12-Dec-03 6:13
memberJohn M. Drescher12-Dec-03 6:13 
GeneralRe: How about that Pin
John Carson13-Dec-03 15:50
memberJohn Carson13-Dec-03 15:50 
AnswerRe: How about that Pin
John M. Drescher12-Dec-03 6:11
memberJohn M. Drescher12-Dec-03 6:11 
AnswerRe: How about that Pin
RabidCow13-Dec-03 7:50
memberRabidCow13-Dec-03 7:50 
QuestionWrong section? Pin
dog_spawn12-Dec-03 5:48
memberdog_spawn12-Dec-03 5:48 
AnswerRe: Wrong section? Pin
Paolo Messina12-Dec-03 8:33
memberPaolo Messina12-Dec-03 8:33 
AnswerRe: Wrong section? Pin
Jörgen Sigvardsson12-Dec-03 11:39
memberJörgen Sigvardsson12-Dec-03 11:39 
GeneralRe: Wrong section? Pin
dog_spawn12-Dec-03 14:11
memberdog_spawn12-Dec-03 14:11 
GeneralRe: Wrong section? Pin
Jörgen Sigvardsson12-Dec-03 14:18
memberJörgen Sigvardsson12-Dec-03 14:18 
GeneralWhat does the 'a' stand for? Pin
dog_spawn12-Dec-03 16:31
memberdog_spawn12-Dec-03 16:31 
GeneralRe: What does the 'a' stand for? Pin
John Carson13-Dec-03 16:57
memberJohn Carson13-Dec-03 16:57 
GeneralMisquoteathon Pin
dog_spawn14-Dec-03 5:15
memberdog_spawn14-Dec-03 5:15 
GeneralRe: Misquoteathon Pin
John Carson14-Dec-03 5:43
memberJohn Carson14-Dec-03 5:43 
GeneralRe: Misquoteathon Pin
vertigo00oo15-Dec-03 4:35
membervertigo00oo15-Dec-03 4:35 
AnswerRe: Wrong section? Pin
Peter Ritchie15-Dec-03 8:46
memberPeter Ritchie15-Dec-03 8: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160204.4 | Last Updated 12 Dec 2003
Article Copyright 2003 by Mr Scotty
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid