Dear firiends ,
tell me that HOW overloaded operator eg [],(),->
how can i overloaded those operator
sanjay
6 1824
Dear firiends ,
tell me that HOW overloaded operator eg [],(),->
how can i overloaded those operator
sanjay
i would like to know the answer to this question two
does anyone knows a solution?
i would like to know the answer to this question two
does anyone knows a solution?
You need to add something like -
double* operator()( int i, int j );
-
to your class. (Replace double* with whatever you deem appropriate for the class.) For instance, I use the operator() as follows in my EasyBMP code: -
BMP
-
{
-
private:
-
RGBApixel** Pixels;
-
// ... more stuff
-
public:
-
RGBApixel* operator()(int i, int j);
-
// ... more stuff
-
};
-
-
RGBApixel* BMP::operator()( int i, int j)
-
{ return &( Pixels[i][j] ); }
-
In use, it works like this: -
BMP SomeImage;
-
SomeImage.ReadFromFile( "blah.bmp" );
-
-
cout << "pixel (3,2): ("
-
<< (int) SomeImage(3,2)->Red << ","
-
<< (int) SomeImage(3,2)->Green << ","
-
<< (int) SomeImage(3,2)->Blue << ")" << endl;
-
I hope that helps point you in the right direction. -- Paul
Dear firiends ,
tell me that HOW overloaded operator eg [],(),->
how can i overloaded those operator
sanjay
below is some sample code from a 2D matrix class I implemented years ago -
// Matrix class
-
// uses an array of pointers p_rows which contains the start address of each row
-
// in stored the array p_matrix, i.e. the address of element [row][column] is
-
// p_rows[row] + column
-
class Matrix // define type Matrix
-
{ // private members
-
string name; // name of array
-
float *p_matrix; // pointer to array of float to hold matrix
-
float **p_rows; // pointer to rows in p_array
-
int columns, rows; // size of matrix(rows, columns)
-
// member function, return reference to array element (row, column)
-
float & index(const int row, const int column) const
-
{ return *(p_rows[row] + column); }
-
-
...
-
operator[] would be (no array bounds checking) -
// return pointer to array element indexed by ex_row, indexed from 1
-
float * operator[](const int ex_row) const
-
{ return (p_rows[ex_row - 1] - 1); }
-
operator() would be (throws exception on array index out of bounds) -
// return reference to array element (ex_row, ex_col), indexed from 1 //
-
// throw exception if array index out of bo //
-
float & Matrix::operator()(int ex_row, int ex_col) const
-
{
-
if ((ex_row < 1) || (ex_col < 1) || (ex_row > rows) || (ex_col > columns))
-
{
-
// error! create message for throwing exception
-
stringstream ss;
-
ss << " Matrix::operator() array " << name <<
-
"(" << ex_row << "," << ex_col << ") out of bounds";
-
string s=ss.str();
-
throw out_of_range(s); // out of bounds exception
-
}
-
return this->index(ex_row - 1, ex_col - 1); // return reference to element
-
}
-
full code is available from
http://www.geocities.com/horacespider/programming/C/matrix.txt
...
Exactly. :) Do note that if you make the function const, then your operator() will only be able to fetch the matrix value, but won't be able to change the matrix value. That's why I tend to not make it const when I write matrix libraries, so that you can use it in a way you'd use it on paper, e.g.,
*Matrix1(3,2) = *Matrix2(4,0);
A very simple 2D array class is included as part of the EasyBMP Extensions. (It doesn't actually rely upon EasyBMP.) It's fully open source, so you're free to look it over and use/abuse it as you see fit. :)
In the way the implementation is done, you can do things like this: -
SimpleArray InputData;
-
InputData.ReadFromFile( "PHI.dat" );
-
SimpleArray OutputData( InputData.Rows, InputData.Cols );
-
int i,j;
-
for( i=0 ; i < InputData.Rows ; i++)
-
{
-
for( j=0 ; j < InputData.Cols ; j++)
-
{
-
*OutputData(i,j) = pow( *InputData(i,j) , 2.0 );
-
}
-
}
-
Horace raised a good point about accessing data elements that are out of bounds. As he pointed out, you can check the bounds when you return the memory address. Another thing you can do, rather than throw and exception, is crop the index and give the user a warning. I do this with the operator() on my EasyBMP library. Here's an adaptation: -
class Matrix
-
{
-
private:
-
int Rows;
-
int Cols;
-
double** Data;
-
// ... etc
-
-
public:
-
double* operator()( int i, int j );
-
// ... etc
-
};
-
-
double* Matrix::operator()( int i, int j )
-
{
-
if( i < 0 || i >= Rows )
-
{
-
cout << "Matrix Warning: index " << i << " was out of bounds!" << endl
-
<< "Cropping to range [0," << Rows-1 << "]" << endl;
-
if( i < 0 ){ i = 0; }
-
if( i >= Rows ){ i = Rows-1; }
-
}
-
if( j < 0 || j >= Cols )
-
{
-
cout << "Matrix Warning: index " << j << " was out of bounds!" << endl
-
<< "Cropping to range [0," << Cols-1 << "]" << endl;
-
if( j < 0 ){ j = 0; }
-
if( j >= Cols ){ j = Cols-1; }
-
}
-
return &( Data[i][j] );
-
}
-
Do note, however, that checks like these will slow the program down. If you know for certain that the programmer will only access correct matrix entries, you can cut those checks to speed up execution. -- Paul
macklin01 makes a good point about operator[] - in practice many functions need two versions one for 'normal' obects and one for constant objects,e.g in <vector>
http://www.cppreference.com/cppvector/vector_operators.html
shows -
TYPE& operator[]( size_type index );
-
const TYPE& operator[]( size_type index ) const;
-
also the point about error checking is critical - the more you do the longer the execution time. e.g. in <vector> to access an element you can use at() or [] but
http://www.cppreference.com/cppvector/at.html
states "The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector."
macklin01 makes a good point about operator[] - in practice many functions need two versions one for 'normal' obects and one for constant objects,e.g in <vector>
http://www.cppreference.com/cppvector/vector_operators.html
shows -
TYPE& operator[]( size_type index );
-
const TYPE& operator[]( size_type index ) const;
-
also the point about error checking is critical - the more you do the longer the execution time. e.g. in <vector> to access an element you can use at() or [] but
http://www.cppreference.com/cppvector/at.html
states "The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector.", i.e. [] does no array bounds checking.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nitin Bhardwaj |
last post by:
Thanx in advance for the response...
I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.
...
|
by: Andy Jarrell |
last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain
overloaded operators don't seem to come with the inheritance. For example:
...
|
by: masood.iqbal |
last post by:
I have a few questions regarding overloaded typecast
operators and copy constructors that I would like an
answer for. Thanks in advance.
Masood
(1) In some examples that I have seen...
|
by: ambar.shome |
last post by:
Hi,
As you know there are few operators in C++ which cant be overloaded.
They are:
.., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
dynamic_cast , const_cast ,...
|
by: Alex Zhitlenok |
last post by:
Hi,
My question is how to resolve in C# ambiguous overloaded operators?
Let say, I have two unrelated classes A and B, each one implements
overloaded operator + with the first parameter of type...
|
by: cybertof |
last post by:
Hello,
Is it possible in C# to have 2 overloaded functions with
- same names
- same parameters
- different return type values
If no, is it possible in another language ?
|
by: B. Williams |
last post by:
I have an assignment for school to Overload the operators << and >and I
have written the code, but I have a problem with the insertion string
function. I can't get it to recognize the second of...
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: rohits123 |
last post by:
I have an overload delete operator as below
//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
|
by: subramanian100in |
last post by:
overloaded operator=()
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
If...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |