468,738 Members | 2,515 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

How to implement Overloading Operator () in C#

I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code.
I don't really know how to implement the operator = and operator () in C#.


#ifndef _OFFMAT_HPP_
#define _OFFMAT_HPP_

#include "fdct_wrapping_inc.hpp"
using std::ostream;
using std::istream;
using std::ios_base;

FDCT_WRAPPING_NS_BEGIN_NAMESPACE

//-------------------------------------------------
template <class F>
class OffMat {
public:
int _m, _n; //size
int _s, _t; //offset
F* _data; //data stored in fortran style
public:
OffMat(int m=0, int n=0): _m(m), _n(n), _s(-m/2), _t(-n/2) {
if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data = NULL;
}
OffMat(const OffMat& C): _m(C._m), _n(C._n), _s(C._s), _t(C._t) {
if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data = NULL;
if(_m>0 && _n>0) { memcpy( _data, C._data, _m*_n*sizeof(F) ); }
}
~OffMat() {
if(_m>0 && _n>0) { delete[] _data; _data = NULL; }
}
OffMat& operator=(const OffMat& C) {
if(_m>0 && _n>0) { delete[] _data; _data = NULL; }
_m = C._m; _n = C._n; _s = C._s; _t = C._t;
if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data = NULL;
if(_m>0 && _n>0) { memcpy( _data, C._data, _m*_n*sizeof(F) ); }

return *this;
}
void resize(int m, int n) {
if(_m!=m || _n!=n) {
if(_m>0 && _n>0) { delete[] _data; _data = NULL; }
_m = m; _n = n; _s = -m/2; _t = -n/2;
if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data = NULL;
}
}
const F& operator()(int i, int j) const {
assert( i>=_s && i<_m+_s && j>=_t && j<_n+_t );
return _data[(i-_s) + (j-_t)*_m];
}
F& operator()(int i, int j) {
assert( i>=_s && i<_m+_s && j>=_t && j<_n+_t );
return _data[(i-_s) + (j-_t)*_m];
}

int m() const { return _m; }
int n() const { return _n; }
int s() const { return _s; }
int t() const { return _t; }
F* data() const { return _data; }
};

//OUTPUT
template <class F> inline ostream& operator<<( ostream& os, const OffMat<F>& mat)
{
os<<mat.m()<<" "<<mat.n()<<" "<<mat.s()<<" "<<mat.t()<<endl;
for(int i=mat.s(); i<mat.s()+mat.m(); i++) {
for(int j=mat.t(); j<mat.t()+mat.n(); j++)
os<<" "<<mat(i,j);
os<<endl;
}
return os;
}
//SET VALUE
template <class F> inline void setvalue(OffMat<F>& mat, F val)
{
for(int i=mat.s(); i<mat.s()+mat.m(); i++)
for(int j=mat.t(); j<mat.t()+mat.n(); j++)
mat(i,j) = val;
}
//CLEAR
template <class F> inline void clear(OffMat<F>& M)
{
memset(M.data(), 0, M.m()*M.n()*sizeof(F));
}

typedef OffMat<int> IntOffMat;
typedef OffMat<double> DblOffMat;
typedef OffMat<cpx> CpxOffMat;

FDCT_WRAPPING_NS_END_NAMESPACE

#endif
Aug 24 '07 #1
0 1748

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by | last post: by
5 posts views Thread by luca regini | last post: by
3 posts views Thread by y-man | last post: by
2 posts views Thread by Colonel | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.