简体   繁体   中英

passing variable without a name to function

I saw a function written in the following manner:

retVal PreProcessor::TxSlotCB(void *a_pClass, PRE_PARAMS &/*commonParam*/)
{
 some code
}

struct PRE_PARAMS 
{
 uint32_t param1;
 uint32_t param2;
};

what is happening in the second parameter? how can it be empty? and is there any way to access to it?

In C++, a formal parameter can be given but anonymous. This means that the actual argument should be given but is not used in the called function.

So it should be given in calling context, it is transmitted, but the called function cannot and does not use it. And the compiler won't give any warnings.

You cannot access it in the body of the function. If you need to access it, change the declaration of the formal parameter to give it some name.

This means that parameter of type PRE_PARAM is not used by this function currently.

So, what happens is when you design a function you decide on the parameters this function would take to fulfill it's purpose.

But later you find that this parameter is not of much significance to this function. However, removing this parameter from function declaration is a tedious job as you have to check for all calls to this function and make sure they confirm to that change.

So, a better way is to not provide the name for argument in function's definition making that parameter redundant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM