c ++:用于创建可选参数的输出参数的虚拟参数
c++: dummy argument for output parameter to create optional parameter
是否有一种紧凑的方法可以通过传递一些虚拟对象来跳过函数调用中的输出参数?
在下面的示例中,我想写类似 calc_multiples(myNum, NULL, myNumTimes3)
的内容,因为我不需要第二个参数,因此不想为此定义变量。
In python one would use _
for that..
#include <iostream>
/**
* Calculates multiples of number
*
* @param[in] t1 number to multiply
* @param[out] t2 multiplied by 2
* @param[out] t2 multiplied by 3
*/
void calc_multiples(const int t1, int &t2, int &t3)
{
t2 = 2*t1;
t3 = 3*t1;
return;
}
int main()
{
int myNum = 7;
int myNumTimes2; // I want to avoid this line
int myNumTimes3;
calc_multiples(myNum, myNumTimes2, myNumTimes3);
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
没有直接替代 Python 的 _
。
应重写该函数以采用指针而不是引用,并且在写入指针之前应检查指针是否为空。
然后你将nullptr
传递给你想跳过的参数。
问题作者 (Markus Dutschke) 的评论
代码如下所示
#include <iostream>
/**
* Calculates multiples of number
*
* THIS IS JUST FOR DEMONSTRATION.
* OF COURSE ONE WOULD USE DEFAULT ARGUMENTS IN THIS CASE.
*
* @param[in] t1 number to multiply
* @param[out] t1 multiplied by 2 (optional argument)
* @param[out] t1 multiplied by 3 (mandatory argument)
*/
void calc_multiples(const int t1, int *t2, int &t3)
{
if (t2 != nullptr)
{
*t2 = 2 * t1;
}
t3 = 3 * t1;
return;
}
int main()
{
int myNum = 7;
int myNumTimes2; // I want to avoid this line
int myNumTimes3;
calc_multiples(myNum, nullptr, myNumTimes3);
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
您可以简单地定义另一个函数来省略不需要的参数:
#include <iostream>
/**
* Calculates multiples of number
*
* @param[in] t1 number to multiply
* @param[out] t2 multiplied by 2
* @param[out] t2 multiplied by 3
*/
void calc_multiples(const int t1, int &t2, int &t3)
{
t2 = 2*t1;
t3 = 3*t1;
return;
}
void calc_multipleOf3(const int t1, int &t3)
{
int ignored;
calc_multiples(t1, ignored, t3);
}
int main()
{
int myNum = 7;
int myNumTimes3;
calc_multipleOf3(myNum, myNumTimes3)
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
或者:
#include <iostream>
/**
* Calculates multiples of number
*
* @param[in] t1 number to multiply
* @param[out] t2 multiplied by 2
* @param[out] t2 multiplied by 3
*/
void calc_multiples(const int t1, int &t2, int &t3)
{
t2 = 2*t1;
t3 = 3*t1;
return;
}
int main()
{
int myNum = 7;
int myNumTimes3;
auto calc_multipleOf3 = [](const int t1, int &t3) {
int ignored;
calc_multiples(t1, ignored, t3);
};
calc_multipleOf3(myNum, myNumTimes3)
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
是否有一种紧凑的方法可以通过传递一些虚拟对象来跳过函数调用中的输出参数?
在下面的示例中,我想写类似 calc_multiples(myNum, NULL, myNumTimes3)
的内容,因为我不需要第二个参数,因此不想为此定义变量。
In python one would use _
for that..
#include <iostream>
/**
* Calculates multiples of number
*
* @param[in] t1 number to multiply
* @param[out] t2 multiplied by 2
* @param[out] t2 multiplied by 3
*/
void calc_multiples(const int t1, int &t2, int &t3)
{
t2 = 2*t1;
t3 = 3*t1;
return;
}
int main()
{
int myNum = 7;
int myNumTimes2; // I want to avoid this line
int myNumTimes3;
calc_multiples(myNum, myNumTimes2, myNumTimes3);
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
没有直接替代 Python 的 _
。
应重写该函数以采用指针而不是引用,并且在写入指针之前应检查指针是否为空。
然后你将nullptr
传递给你想跳过的参数。
问题作者 (Markus Dutschke) 的评论
代码如下所示
#include <iostream>
/**
* Calculates multiples of number
*
* THIS IS JUST FOR DEMONSTRATION.
* OF COURSE ONE WOULD USE DEFAULT ARGUMENTS IN THIS CASE.
*
* @param[in] t1 number to multiply
* @param[out] t1 multiplied by 2 (optional argument)
* @param[out] t1 multiplied by 3 (mandatory argument)
*/
void calc_multiples(const int t1, int *t2, int &t3)
{
if (t2 != nullptr)
{
*t2 = 2 * t1;
}
t3 = 3 * t1;
return;
}
int main()
{
int myNum = 7;
int myNumTimes2; // I want to avoid this line
int myNumTimes3;
calc_multiples(myNum, nullptr, myNumTimes3);
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
您可以简单地定义另一个函数来省略不需要的参数:
#include <iostream>
/**
* Calculates multiples of number
*
* @param[in] t1 number to multiply
* @param[out] t2 multiplied by 2
* @param[out] t2 multiplied by 3
*/
void calc_multiples(const int t1, int &t2, int &t3)
{
t2 = 2*t1;
t3 = 3*t1;
return;
}
void calc_multipleOf3(const int t1, int &t3)
{
int ignored;
calc_multiples(t1, ignored, t3);
}
int main()
{
int myNum = 7;
int myNumTimes3;
calc_multipleOf3(myNum, myNumTimes3)
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}
或者:
#include <iostream>
/**
* Calculates multiples of number
*
* @param[in] t1 number to multiply
* @param[out] t2 multiplied by 2
* @param[out] t2 multiplied by 3
*/
void calc_multiples(const int t1, int &t2, int &t3)
{
t2 = 2*t1;
t3 = 3*t1;
return;
}
int main()
{
int myNum = 7;
int myNumTimes3;
auto calc_multipleOf3 = [](const int t1, int &t3) {
int ignored;
calc_multiples(t1, ignored, t3);
};
calc_multipleOf3(myNum, myNumTimes3)
std::cout << "my number times 3: " << myNumTimes3;
return 0;
}