C++ 从指针数组的元素传递地址以设置另一个指针地址

C++ 从指针数组的元素传递地址以设置另一个指针地址,c++,pointers,reference,C++,Pointers,Reference,我试着搜索答案,但不幸的是,例子太复杂了,我无法理解。最基本的是,我想知道如何传递和更改指针数组的地址 例如: 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 elemen

我试着搜索答案,但不幸的是,例子太复杂了,我无法理解。最基本的是,我想知道如何传递和更改指针数组的地址

例如:

    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);
   }

问题1。下面两个是正确的。arrayM[ind]将返回指针。因此,请认为您正在将数据存储到指针中。请注意,要添加的对象(即newM)的生存期

问题2。以下是正确的

out = arryM[ind];
此外,当指针需要引用时,不能返回指针。所以,您可以将函数修改为发送方指针,也可以将值改为发送方指针

第三季度。如果您的回信地址如下

return array[ind]
1. myArray[index]
2. *(myArray+1)
然后返回一个指针。(即M*myArray)。您可以按如下方式访问元素

return array[ind]
1. myArray[index]
2. *(myArray+1)
如果返回引用(即M&),则必须在引用或普通对象中收集它们,否则也可以按如下方式将值存储到指针中

Function: M& foo()
Call: M m = foo()
OR M& m = foo()
OR M* m;  *m = foo();
问题5。我无法理解你的问题。我稍后会更新它


如果您需要详细解释,请告诉我。

这里的问题实在太多了。理想情况下,你的帖子只涉及一个非常具体的问题。如果你愿意,我们可以在聊天中讨论:非常感谢!我意识到有很多问题,但在我看来,它们都是紧密联系在一起的。因此,如果我只收到一些答案/澄清,其余的答案将被解开,并对我来说是显而易见的。。。不过,我为我在论坛上的新手身份道歉!非常感谢!!我没想到会有如此迅速的反应!Q1回答得很好,谢谢!第二季度/第三季度也得到了很好的解决,谢谢!Q4我想我明白了,但我想确保“*m=foo()”意味着地址m是对象在内存中的地址..或者,使用int数据类型:function:int&foo(){int k=3;int&l=k;return l;}call:int n=1;int*p;p=&n;库特