I’ve tried searching through responses, but examples unfortunately seem too complex for me to understand. Most fundamentally I want to know how I can pass and change the addresses of an array of pointers.
Example:
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);
}
So my 5 main questions extracted from the code sample are below, and Q1, Q3, Q4 are most important/confusing to me:
Q1 (important): if an array[i] returns a pointer *n, then how can we dereference “array[i]” to look like “n” so we can get “n=p” where “*p” has an address of an externally created object. Also, where “*p” is passed through a function call with type& parameter.
Q2 : in the given context, is it better to return a pointer or an address in the function? Or would it be easier to change the context to work with one function return type over the other?
Q3 (important): if we are returning an address, what is the correct way to get “array[i]” (based on same assumption as in Q1) to be the address contained in “n” and not object represented by “*n”?
Q4 (important): if a function is returning an address &, then is a locally created pointer *p able to receive an address by “p = obj.funcretaddress()”? I saw some example code with doubles though that made me confused: “double hours = funcreturningaddress()”.
Q5 : just double checking that if a function parameter takes “obj &x” and we locally create a “obj *y” that in the function call we would pass “function(*y)” and then within the function scope “obj *n = &x” so that a pointer represented by array[i] could take correct address of the locally created obj.
Many thanks, as I have been compiling and tweaking for several days now, getting confused trying to take examples with int and double data types from the web and apply them to created class objects.
My apologies and please let me know if anything unclear!
edit: Many thanks, and just wanted to clarify the Q4:
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?
}
Q1. Following two are correct. arrayM[ind] will return pointer. So think that you are storing into the pointer. Be careful, about the lifetime of object you are adding (i.e. newM)
Q2. Following is correct.
Additionally, you can not return pointer when it is expecting reference. So either you can modify function to sender pointer or send value instead of pointer.
Q3. If you are returning address as follows
then that would return a pointer. (i.e. M* myArray). You can access elements as follows
Q4.If you returning reference (i.e M&) then you must collect them in either reference or normal object otherwise you can also store the value into pointer as follows.
Q5. I could not get your question here. I shall update it later.
Let me know if you need detailed explanation.