# matrix assignment operator

This topic is 4159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have an assignment operator "=" for 4x4 matrices
	CMatrix operator=(CMatrix param){
CMatrix temp;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
temp.SetValue(i,j,param.GetValue(i,j));
}
}

return temp;
}


(the function set value takes in the colums and row and value(in that order) the function get value takes in the column and row(in that order)) it isnt quite working how i expected it to the temp matrix holds exactly what it is supposed to but it just doesnt return the value for example using single numbers
  int A,B;
A = 2;
B = 3;
A = B;

cout << A;


when the values of a is printed out it is still 2 when it should be 3 i have checked the internal workings of the opertor overload and as i said the temp matrix is indeed holding the correct information so i assume i am just returning it wrong if anyone can shed some light it would be most appreciated if you need any more info just ask :) thank you

##### Share on other sites
Well you haven't assigned anything to the matrix so why would its value change? You should be assigning param to this not creating some temporary matrix which is a copy of param.

##### Share on other sites
When you use the operator that way you will have to set the actual values of the class that is housing the operator, so you wouldn't want temp there, you want to use the this pointer, or more realistically just call the function.

##### Share on other sites

	void operator=(CMatrix param){		for(int i = 0;i<4;i++){			for(int j = 0;j<4;j++){				this->SetValue(i,j,param.GetValue(i,j));			}		}	}

so it should be like it is above?

##### Share on other sites
Yes but No you should still be returning a CMatrix& (please I'm begging you drop the C prefix ;) ) in particular you should return *this so that you can chain things like a = b = c;

##### Share on other sites

the C prefix wasn't put there by me its a collaborative effort and somebody
else had already started the frame work i m just doing the implementation however
i also hate the prefix and i'll remove it as soon as i get it all working properly :)

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009716
• ### Who's Online (See full list)

There are no registered users currently online

×