Click here to Skip to main content
Licence 
First Posted 11 Dec 2003
Views 83,580
Bookmarked 16 times

How to implement a default value for a struct parameter

By | 11 Dec 2003 | Article
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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPerformance issue PinmemberRoman Keskenti [SmoCoder]9:13 14 Dec '03  
QuestionHow about that PinmemberFranz Brunner4:58 12 Dec '03  
AnswerRe: How about that PinmemberMr Scotty5:07 12 Dec '03  
GeneralRe: How about that PinmemberJohn M. Drescher5:13 12 Dec '03  
GeneralRe: How about that PinmemberJohn Carson14:50 13 Dec '03  
AnswerRe: How about that PinmemberJohn M. Drescher5:11 12 Dec '03  
AnswerRe: How about that PinmemberRabidCow6:50 13 Dec '03  
QuestionWrong section? Pinmemberdog_spawn4:48 12 Dec '03  
AnswerRe: Wrong section? PinmemberPaolo Messina7:33 12 Dec '03  
AnswerRe: Wrong section? PinmemberJörgen Sigvardsson10:39 12 Dec '03  
GeneralRe: Wrong section? Pinmemberdog_spawn13:11 12 Dec '03  
GeneralRe: Wrong section? PinmemberJörgen Sigvardsson13:18 12 Dec '03  
GeneralWhat does the 'a' stand for? Pinmemberdog_spawn15:31 12 Dec '03  
GeneralRe: What does the 'a' stand for? PinmemberJohn Carson15:57 13 Dec '03  
GeneralMisquoteathon Pinmemberdog_spawn4:15 14 Dec '03  
GeneralRe: Misquoteathon PinmemberJohn Carson4:43 14 Dec '03  
GeneralRe: Misquoteathon Pinmembervertigo00oo3:35 15 Dec '03  
AnswerRe: Wrong section? PinmemberPeter Ritchie7:46 15 Dec '03  

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.

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