473,388 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 1994

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

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.