• 15
• 15
• 11
• 9
• 10

# default parameter

This topic is 4478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

void f(int i =0, long l =11, double d = 111) { do something. } //call it I just want to alter d = 112 ,I must assignment i l too?? f(i=0,l=11,d=112); I want this form. f(,,d=112);

##### Share on other sites
In many functions you can chance the order of elements, so that the ones most likely specified will be at start so if d is often specified when calling you could change your function too.
void f(double d = 111,int i =0, long l =11){//do something.}

If you can't do this for whatever reason (for example there might normally be a greater chance i is specified than d, or you already have lots of code calling the function) you need to specify all parameters until d.

##### Share on other sites
To answer your question succinctly, yes, I believe if you want to pass in a value for d, you must also pass in values for i and l. You could however rearrange the definition of the variables to avoid this, as CTar suggests.

##### Share on other sites
Yea, you cannot "skip" arguments with default values. In a call to f(a,x) the compiler couldn't tell if you meant f(a,(default),x) or f(a,x,(default)). Perhaps if there were some syntax like f(a,,x) this would be possible, but there isn't (and never will be).

If you rearrange the arguments of f to be (double d,int i, long l) (for example) you may still end up with the problem of having to specify l without specifying d or i. If you encounter this problem often, then default values are not the solution. Instead you want to look at function overloading. Instead of specifying default values for f in the prototype, you provide multiple prototypes of f taking the appropriate arguments. These "overloaded" functions can just call the most-generic version of f that takes all your arguments, and can substitute default values for the appropriate missing arguments:

// most genericvoid f(int i,long l,double d) { ... }// if you only want to specify i...void f(int i) { f(i,11,111.0); }// if you only want to specify i and d...void f(int i,int d) { f(i,11,d); }

Of course, this quickly becomes a bit of a maintenance nightmare as the size of your functions parameter list grows, so its not a recommended solutions except possibly for small functions like this.

The option is, of course, to bite the bullet and just specify values for all arguments of f. Perhaps it would be beneficial if you gave us some context for f, then we might be able to suggest a better solution.

##### Share on other sites
you could always use http://www.boost.org/libs/parameter/doc/html/
Boost parameter library.
Probably over kill, but its there...

##### Share on other sites
Quote:
 Original post by jpetrieYea, you cannot "skip" arguments with default values. In a call to f(a,x) the compiler couldn't tell if you meant f(a,(default),x) or f(a,x,(default)). Perhaps if there were some syntax like f(a,,x) this would be possible, but there isn't (and never will be). If you rearrange the arguments of f to be (double d,int i, long l) (for example) you may still end up with the problem of having to specify l without specifying d or i. If you encounter this problem often, then default values are not the solution. Instead you want to look at function overloading. Instead of specifying default values for f in the prototype, you provide multiple prototypes of f taking the appropriate arguments. These "overloaded" functions can just call the most-generic version of f that takes all your arguments, and can substitute default values for the appropriate missing arguments:// most genericvoid f(int i,long l,double d) { ... }// if you only want to specify i...void f(int i) { f(i,11,111.0); }// if you only want to specify i and d...void f(int i,int d) { f(i,11,d); }Of course, this quickly becomes a bit of a maintenance nightmare as the size of your functions parameter list grows, so its not a recommended solutions except possibly for small functions like this.The option is, of course, to bite the bullet and just specify values for all arguments of f. Perhaps it would be beneficial if you gave us some context for f, then we might be able to suggest a better solution.

IDirect3DDevice9* D3DDevice::create( UINT adapt, D3DDEVTYPE devType ,bool nvidiaAdapt){	//create drive	m_drive = Direct3DCreate9(D3D_SDK_VERSION);	// &#26816;&#27979; adapt&#26159;&#21542;&#23384;&#22312; &#36824;&#26377;&#22810;&#19968;&#20010;&#25511;&#21046;&#19981;&#31649;&#23384;&#22312;&#21542; &#37117;&#21487;&#20197;&#31105;&#27490;	// 2&#37325;&#26816;&#27979;	if (nvidiaAdapt) // 1	{		// Look for 'NVIDIA NVPerfHUD' adapter		int inum = m_drive->GetAdapterCount();		for (int Adapter = 0; Adapter < inum; Adapter++)		{			D3DADAPTER_IDENTIFIER9 Identifier;			HRESULT Res = m_drive->GetAdapterIdentifier(Adapter, 0, &Identifier);			if (strcmp(Identifier.Description, "NVIDIA NVPerfHUD") == 0) // 2			{				adapt = Adapter;				devType = D3DDEVTYPE_REF;			}		}	}	// hardware vp?	D3DCAPS9 caps;	m_drive->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);	if (!(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT))		 m_vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;	//offer info	m_pp.BackBufferWidth            = m_width;	m_pp.BackBufferHeight           = m_height;	m_pp.BackBufferFormat           = D3DFMT_A8R8G8B8;	m_pp.BackBufferCount            = 1;	m_pp.MultiSampleType            = D3DMULTISAMPLE_NONE;	m_pp.MultiSampleQuality         = 0;	m_pp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 	m_pp.hDeviceWindow              = m_hwnd;	m_pp.Windowed                   = m_windowed;	m_pp.EnableAutoDepthStencil     = true; 	m_pp.AutoDepthStencilFormat     = D3DFMT_D24S8;	m_pp.Flags                      = 0;	m_pp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;	m_pp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;		//create d3d device	HRESULT hr = m_drive->CreateDevice(adapt, devType, m_hwnd, m_vp, &m_pp, &m_device);	if ( FAILED(hr) )	{		::MessageBox(0, "create direct3d device failed! - derek7", 0, 0);		exit(0);	}	return m_device;}
I want to use a bool nvidiaAdapt to decide if I create a nvidia card for NVPerfHUD test
In offical web,the macro is used to do this job,maybe this is better solution.