Thread: oops? not sure. operator overloading (possible) problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    52

    oops? not sure. operator overloading (possible) problem

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

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    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.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    well i haven't learned templates (knew only C until last week) but umm... what do u mean out of scope?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    huh?
    i don't have a copy constructor......

    what part was wrong?

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    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.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    ?????????????????
    so variables go out of scope after conditionals? bleh. i never knew that.... or didn't remember. probably the first

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    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!!

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM