[英]Pointer function confusion C++
我正在通过“跳入C ++”的方式进行工作,我刚刚到达了有关指针的部分,因此,我的第一面墙。 我正在尝试解决此问题:
问题13.4
编写一个函数,该函数接受两个输入参数并向调用者提供两个单独的结果,一个是两个参数相乘的结果,另一个是将它们相加的结果。 由于您只能直接从函数返回一个值,因此需要通过指针或引用参数返回第二个值。
我只是真的不明白这个问题。 我需要编写一个函数,例如:
int function(int x, int y){
int addition = x + y;
int multi = x * y;
}
但是由于我不完全理解这个问题,所以我不知道如何适应指针。 如果有人能为我哑巴,我将不胜感激。
谢谢你的时间!
你可以试试
int functionPtr(int x, int y, int* addition) {
// As JustSid mentioned in the comment, it would be good to check the pointer first
if (addition) {
*addition = x + y;
}
return x * y;
}
要么
int functionRef(int x, int y, int& addition){
addition = x + y;
return x * y;
}
当你想给他们打电话时
int x = 1;
int y = 2;
int addition1 = 0;
int multi = functionPtr(x, y, &addition1);
int addition2 = 0;
int multi = functionRef(x, y, addition2);
这意味着呼叫者为其他结果提供了空间。
例:
int function(int x, int y, int& res2)
{
res2=x+y;
return x*y;
}
有点离题,因为我不太喜欢问题13.4提出的问题……如果需要,您可以返回整个对象。 考虑:
class AddMul {
public:
int added;
int multiplied;
};
AddMul function(int x, int y) {
AddMul am;
am.added = x + y;
am.multiplied = x * y;
return am;
}
当然,那么您可以考虑其他一些有趣的事情,例如AddMul类是否包含该函数作为成员? 还是直接是构造函数?
class AddMul {
public:
int added;
int multiplied;
public:
AddMul(int x, int y) {
added = x + y;
multiplied = x * y;
}
int getAdd() const {
return added;
}
int getMul() const {
return multiplied;
}
};
无论如何,要考虑一些事情。
这不是本书要你做的,但是值得一提的是提供指针/引用以从一个函数中返回多个结果的替代方法。
std::pair<int, int> function(int x, int y)
{
std::pair<int, int> p;
p.first = x + y;
p.second = x * y;
return p;
}
我对C ++超级生锈,所以如果我的回答有点不对,请原谅我,但基本上,指针是提供某些数据地址的变量,而不是数据本身。 这意味着您可以将整数的地址传递给函数,以允许该函数随后跟随指针并将数据存储在该整数的存储位置中。 要获取变量或对象的地址,请使用与号(&),并跟随指针使用星号(*)
例:
int main(void){
int x = 2;
int y = 3;
int multiplicationResult = 0;
int additionResult = twoOperations(x, y, &multiplicationResult); // The ampersand allows us to pass in a pointer to the memory address of multiplicationResult, rather than the value currently stored in it (0)
// additionResult will now be 5
// multiplicationResult will now be 6 - our function was able to store the result in memory using the pointer so we didn't need to use an assigment statement.
}
int twoOperations(int x, int y, int *multiResult){
int addition = x + y;
*multiResult = x * y; // The asterisk basically means "follow this pointer and store the result in that memory location."
return addition;
}
在向自己阅读代码时,我首先学会了如何通过大声朗读自己的符号来处理指针,如下所示:* =“跟随指针指向...”&=“ ...的内存地址”
希望这可以帮助!
这是文本的重要部分:
编写一个带有两个输入参数并向调用者提供两个单独结果的函数[...]
这意味着sumAndProduct
的调用者可以调用它来接收sum和product,这种虚构的语法应传达以下含义:
int main() {
int sum;
int product
(sum, product) = sumAndProduct(29, 541);
printf("the sum of the two numbers is %d and the product is %d\n", sum, product);
}
但是,C中不存在这样的语法。 练习要求您找到一种使用现有C语法的方法(提示:函数参数的类型可以为int*
或int&
。)
编辑:您可以使用一个带有4个参数的函数,其中两个是输入参数,两个是输出参数; 因此,它具有一个4-arg函数,该函数正好接受两个输入参数。
由于问题是“带有两个输入参数的函数”,因此函数中不能有3个参数。 实现此目的的一种方法是将指针返回到数组。 所以
int * function(int x, int y) {
int * res = new int[2];
//Perform computation
//res[0] = ...
//res[1] = ...
return res;
}
请注意,您应该在调用函数中释放数组内存。
int function(int a, int &b){
if (a == 0 || b == 0)
{
int bb = b;
b = 0;
return a+bb;
}
b = (b*a);
return a+(b/a);
}
这可能不好,但是它只有两个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.