Sign in to follow this  
Wardyahh

matrix assignment operator

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
thx for all your help

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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this