我尝试过搜索回复,但遗憾的是,我无法理解这些例子。从根本上说,我想知道如何传递和更改指针数组的地址。
示例:
class Foo {
M** arryM;
...
arryM = new M*[10];
...
void Foo::replace(int ind, M &newm) {
// Q1: which one correct to change the address of an element to a new object's address?
arryM[ind] = newm; // this correct to pass and reset a new address for pointer?
arryM[ind] = &newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
*arryM[ind] = newm; // or do I need to dereference the pointer rep by array element first?
&arryM[ind] = &newm; // or is this correct??
}
...
// Q2: is it easier to return a pointer or address for this function?
M & Foo::getM(int ind) {
M *out;
// Q3: which one of following correct to get correct address value to return?
out = arryM[ind]; // get address to M obj pointed by pointer array elem
out = *arryM[ind]; // is "arryM[ind]" == "*ptr"?? if so how to get to "ptr"?
out = &arryM[ind]; // is this way to get to "ptr" so addresses passed?
return out;
}
}
void main() {
M *op1;
M *op2;
M *sol;
Foo mstorage; // and assume already got valid M objects pointed to by arryM[] elements
...
// Q4: which one correct?
op1 = mstorage.getM(x); // x being the correct and valid int index pos
op1 = *mstorage.getM(x); // so address and not object pointed to is passed?
*op1 = mstorage.getM(x);
op2 = mstorage.getM(y);
... // perform some operations using op1 and op2 M functions
int size = op1->getDim() + op2->getDim();
sol = new M(size, name); // no default constructor--M objects need a size parameter
...// perform/set values of sol based upon op1, op2 values
// Q5: this is correct to pass the address for sol object created, right?
if (<sol already exists>) mstorage.replace(indx, *sol);
else mstorage.add(*sol);
}
所以从代码示例中提取的5个主要问题如下:Q1,Q3,Q4对我来说最重要/最让人困惑:
Q1(重要):如果一个数组[i]返回一个指针* n,那么我们如何解除引用“array [i]”看起来像“n”所以我们可以得到“n = p”其中“* p “具有外部创建的对象的地址。此外,“* p”通过类型&amp;的函数调用传递。参数。
Q2:在给定的上下文中,在函数中返回指针或地址是否更好?或者更容易更改上下文以使用一个函数返回类型而不是另一个?
Q3(重要):如果我们返回一个地址,那么获取“array [i]”(基于与Q1中相同的假设)的正确方法是“n”中包含的地址而不是对象表示通过“* n”?
Q4(重要):如果一个函数返回一个地址&amp;,那么是一个本地创建的指针* p能够通过“p = obj.funcretaddress()”接收一个地址吗?我看到一些带有双打的示例代码虽然让我感到困惑:“double hours = funcreturningaddress()”。
Q5:只是仔细检查一下,如果一个函数参数采用“obj&amp; x”,我们在本地创建一个“obj * y”,在函数调用中我们将传递“function(* y)”然后在函数内范围“obj * n =&amp; x”,以便array [i]表示的指针可以获取本地创建的obj的正确地址。
非常感谢,因为我已经编译和调整了好几天,因为尝试从Web获取int和double数据类型的示例并将它们应用于创建的类对象而感到困惑。
道歉,如果有任何不清楚的地方,请告诉我!
编辑:非常感谢,只想澄清问题4:
int a = 2;
int *b;
b = &a;
*b = 3;
int c = 4;
b = func(c); // *b now points to c, and value is 5?
*b = func(c); // *b value is updated to 5, and a therefore also now 5??
...
int & func(int &d) {
d++;
return &d; // correct, or wrong as &&d is returned?
return d; // and the & is applied to the var d?
}
答案 0 :(得分:0)
Q1。以下两个是正确的。 arrayM [ind]将返回指针。所以认为你存储在指针中。小心,关于你要添加的对象的生命周期(即newM)
arryM[ind] = &newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
*arryM[ind] = newm;
Q2。以下是正确的。
out = arryM[ind];
此外,在期望引用时,您无法返回指针。因此,您可以将函数修改为发送者指针或发送值而不是指针。
Q3。如果您要返回地址如下
return array[ind]
然后会返回一个指针。 (即M * myArray)。您可以按如下方式访问元素
1. myArray[index]
2. *(myArray+1)
问题4.如果您返回引用(即M&amp;),则必须在引用或普通对象中收集它们,否则您也可以将值存储到指针中,如下所示。
Function: M& foo()
Call: M m = foo()
OR M& m = foo()
OR M* m; *m = foo();
Q5。我无法在这里得到你的问题。我稍后会更新。
如果您需要详细解释,请告诉我。