Sign in to follow this  

default parameter

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
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 this post


Link to post
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 generic
void 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 this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
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 generic
void 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);

// 检测 adapt是否存在 还有多一个控制不管存在否 都可以禁止
// 2重检测
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this