Using operator "()" inside boost matrix

This is a discussion on Using operator "()" inside boost matrix within the C++ Programming forums, part of the General Programming Boards category; Hi, I've wrote a class inherit from boost matrix, however, I can't figure out how to access the random access ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    33

    Using operator "()" inside boost matrix

    Hi,

    I've wrote a class inherit from boost matrix, however, I can't figure out how to access the random access operator "()" inside the class.

    For instance, I can use "()" outside the class by m.(x, y) = some_value, however, inside the class I can't just using (x, y) = some_value, although my compiler didn't complain this way, some_value will not be assigned successfully.

    Neither this(x, y), *this(x, y), this->(x, y) will pass the compiler. (I have to confess that I'm not familiar with the usage of "this"...)

    Besides, I can't examine the value of x_location and y_location inside the following for-loop, is it a bug of gdb?

    Here is my code, any idea?

    Code:
    #ifndef TARGET_REGION_H
    #define TARGET_REGION_H
    
    #include <iostream>
    #include <vector>
    #include <utility>
    #include <boost/numeric/ublas/matrix.hpp>
    #include <boost/numeric/ublas/io.hpp>
    
    using namespace std ;
    using namespace boost::numeric ;
    
    class Target_Region: public ublas::matrix<bool>
    {
    public:
    	Target_Region( const int width, const int height, const vector<int> to_be_inpaint_vec_x, const vector<int> to_be_inpaint_vec_y )
    	{
    		assert( to_be_inpaint_vec_x.size() == to_be_inpaint_vec_y.size() ) ;
    
    		bool preserve_previous_value = false ;
    		resize( width, height, preserve_previous_value ) ;
    
    		// for each pixel location in to_be_inpaint_vec, markup to true in the target region
    		vector<int>::const_iterator x_location_iter = to_be_inpaint_vec_x.begin() ;
    		vector<int>::const_iterator y_location_iter = to_be_inpaint_vec_y.begin() ;
    		int x_location = 0 ;
    		int y_location = 0 ;
    		for( ; x_location_iter != to_be_inpaint_vec_x.end() ; ++x_location_iter, ++y_location_iter )
    		{
    			x_location = *x_location_iter ;
    			y_location = *y_location_iter ;
    			( x_location, y_location ) = true ;
    		}
    	}
    
    private:
    
    };
    
    
    #endif
    Last edited by jutirain; 02-17-2008 at 09:21 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    For instance, I can use "()" outside the class by m.(x, y) = some_value, however, inside the class I can't just using (x, y) = some_value, although my compiler didn't complain this way, some_value will not be assigned successfully.
    That's because:
    Code:
    ( x_location, y_location ) = true ;
    becomes:
    Code:
    y_location = true ;
    Neither this(x, y), *this(x, y), this->(x, y) will pass the compiler. (I have to confess that I'm not familiar with the usage of "this"...)
    You should use (*this)(x, y) or more explicitly, operator()(x, y).

    By the way, are you sure Boost's matrix classes are designed to be base classes? I would expect them to follow the C++ Standard Library convention, and the standard containers are not designed to be base classes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    33
    Hi laserlight,

    You're right. Using operator()(x, y), my program now executes as expected.
    Thanks a lot.

    However, I'm not very sure about why STL containers should not be base classes.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    However, I'm not very sure about why STL containers should not be base classes.
    Read:
    When should my destructor be virtual?
    Why are destructors not virtual by default?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    33
    Thanks for your information.

    So, if my self-defined container hold an "is-a" relationship (I know I can do it by "has-a" relationship, however, that's awkward) with some STL container, and I want the convenience from inheritance and some self-defined functionality, what may be the better approach?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    So, if my self-defined container hold an "is-a" relationship (I know I can do it by "has-a" relationship, however, that's awkward) with some STL container, and I want the convenience from inheritance and some self-defined functionality, what may be the better approach?
    You can either do the right thing and just use composition ("has-a"), fowarding functions if necessary, or you can ignore the fact that you are deriving from a class that is not designed to be a base class and risk undefined behaviour if someone attempts to use polymorphism through a base class pointer that has delete or delete[] applied to it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21