• 02-07-2006
w00tw00tkab00t
yeah.... can somebody take a quick look at the unfinished code below and let me know if i'm properly overloading the operators? thanks.

Code:

```matrix matrix::operator + (matrix other) {         int y, x;         this->accesserror = false; other.accesserror = false;         if ((this->getrows() == other.getrows()) && (this->getcols() == other.getcols())) {                 matrix temp(this->getrows(), this->getcols());                 for (y = 1; y <= this->getrows(); y++) {                         for (x = 1; x <= this->getcols(); x++) {                                 temp.setelem(y, x, (this->at(y, x) + other.at(y, x)));                                 if ((this->accesserror) || (other.accesserror) || (temp.accesserror)) { break; }                         }                         if ((this->accesserror) || (other.accesserror) || (temp.accesserror)) { break; }                 }         } else {                 matrix temp(1, 1);                 temp.setelem(1, 1, 0);                 this->accesserror = true; other.accesserror = true;         }         return temp; } /*Matrix addition; make sure to check fail() */```

and

Code:

```bool matrix::operator == (matrix other) {         int y, x;         this->accesserror = false; other.accesserror = false;         /*Row and column check */         if (!((this->getrows() == other.getrows()) && (this->getcols() == other.getcols()))) {                 return false;         }         else {                 for (y = 1; y <= this->getrows(); y++) {                         for (x = 1; x <= this->getcols(); x++) {                                 if ((this->at(y, x) != other.at(y, x)) {                                         return false;                                 }                         }                 }         } }```
• 02-07-2006
ChaosEngine
in your op+ you return a variable (temp) that's gone out of scope and will not compile. I'd also consider throwing an exception instead of return a 'null' matrix with an errorbit. Even better would be to use a template for the matrix so adding different sizes won't compile.
Code:

```matrix<2, 1> m1; matrix<2, 5> m2; matrix<> m3 = m1 + m2; /// arrgggh compiler error!```
'better compile-time then run-time and better run-time then never'

it's pretty easy to add two incompatible matrices and not check accesserror.
it's also pretty tedious to do this each time
Code:

```matrix m1, m2; matrix m3 = m1 + m2; if (m3.accesserror ) {   // erm, do something useful }```

in your op== you need a return true at the end of the method.
• 02-07-2006
w00tw00tkab00t
well i haven't learned templates (knew only C until last week) but umm... what do u mean out of scope?
• 02-07-2006
CornedBee
That part was wrong. If the copy constructor works properly, there is nothing to worry about here.

A possible efficiency problem lies in your passing the argument by value. You should change it to const reference.
• 02-07-2006
w00tw00tkab00t
huh?
i don't have a copy constructor......

what part was wrong?
• 02-07-2006
ChaosEngine
Quote:

Originally Posted by CornedBee
That part was wrong. If the copy constructor works properly, there is nothing to worry about here.

A possible efficiency problem lies in your passing the argument by value. You should change it to const reference.

you're right about the reference, but unless I'm completely stoned (and I don't think I am) he's returning a variable that's already gone out of scope
Code:

```// edited for clarity matrix matrix::operator + (matrix other) {     if (someCondition)     {         matrix temp(this->getrows(), this->getcols());         // some stuff     } // temp out of scope here     else     {         matrix temp(1, 1);         // more stuff     } // 2nd temp out of scope     // where'd this come from?     return temp; }```
I am not talking about return a copy of a local temporary (I know how copy ctors work :P ), I'm talking about a syntax error.
• 02-07-2006
w00tw00tkab00t
?????????????????
so variables go out of scope after conditionals? bleh. i never knew that.... or didn't remember. probably the first
• 02-07-2006
ChaosEngine
Quote:

Originally Posted by w00tw00tkab00t
?????????????????
so variables go out of scope after conditionals? bleh. i never knew that.... or didn't remember. probably the first

the {} braces are c/c++'s scope delimiters. anything created within a matching set of {}'s is automatically cleaned up at the end of that set. (in C, local stack variables used to be called auto-vars).

so,
Code:

```void func() {     int outer = 0;     {         int inner = 0;     }// inner dies here       inner = 2; // compile error!!     if (outer)     {           int ifVar = 0;     } // ifVar dies here     ifVar = 2; // compile error!!     while(true)     {           int loopVar = 0;     } // guess what happens to loopVar here?     loopVar = 2; // compile error!! } // outer dies here outer = 2; // compile error!!```
• 02-08-2006
CornedBee
Quote:

Originally Posted by w00tw00tkab00t
?????????????????
so variables go out of scope after conditionals? bleh. i never knew that.... or didn't remember. probably the first

Sorry, you're right. Didn't read the function closely enough.