473,324 Members | 2,567 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,324 software developers and data experts.

How do I write an overloaded indexoperator

Hello Experts!!

I have two small classes called Intvektor and Matris shown at the bottom and
a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns.
This works fine! So that's not the problem.
I just mentioned it here just to get a better understand of the whole
problem

Because I have dynamically allocated memory I must delete all these
dynamically allocated memory.
The constructor of Matris where I allocate dynamically works fine.
The destructor where I delete all the allocated memory also works fine.

Now to my problem I want to be able to use an overloaded indexoperator on my
matris object.
In the main program I want to be able to write a[0][0] = 1; meaning putting
the number 1
in row number index 0 and column number index 0. The object a is an instance
of class Matris.
Both definitions of the Intvektor class and the definition of the Matris
class can be seen at the bottom.
I wrote an overloaded indexoperator in the Intvektor class but that one was
very easy that you can see in the class definition of Intvektor.

So have you any idea how to write an overloaded indexoperator for the Matris
class?
I have no idea so I try to ask you out there.

Many thanks
//Tony
//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
a[0][0] = 1; //I want this to work
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk) //default Constructor and user
defined constructor
{ array = new int[size]; }
~Intvektor() //Destruktor
{ delete array; }

Intvektor(const Intvektor& v) //Copy konstruktor
{ copy(v); }

int operator[](int i) const //oveloaded indexoperator
{ return array[i]; }

int& operator[](int i) //oveloaded indexoperator
{ return array[i]; }

const Intvektor& operator=(const Intvektor& v) //assignment operator
{
if (this != &v)
{
delete []array;
copy(v);
}
return *this;
}

private:

void Intvektor::copy(const Intvektor& v) //is called from both
copy constructor and assignment operator
{
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}

int size;
int* array;
};

//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{ matris = new Intvektor*[0]; }//default constructor

Matris(int rows, int cols) : c_rows(rows), c_columns(cols) //user
defined Constructor
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris[i] = new Intvektor[c_columns];
}

Matris(const Matris& v) //Copy constructor
{ copy(v) //is located in the private section }

~Matris() //Destruktor
{ remove(); //Is located in the private section }

const Matris& operator=(const Matris& v) // assignment operator
{
if (this != &v)
{
remove();
copy(v);
}
return *this;
}
private:

void copy(const Matris& v) // is called from both copy constructor
and assignemt operator
{
c_rader = v.c_rader;
c_kolumner = v.c_kolumner;
matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}

void remove() // is called from both destructor and assignment
operator
{
for (int i=0; i< c_rader; i++)
delete[] matris[i];
delete[] matris;
}

int c_rows, c_columns;
Intvektor **matris;
};
Jul 23 '05 #1
2 1659
Tony Johansson wrote:
Hello Experts!!

(snip)

Now to my problem I want to be able to use an overloaded indexoperator on
my matris object.
In the main program I want to be able to write a[0][0] = 1; meaning
putting the number 1
in row number index 0 and column number index 0. The object a is an
instance of class Matris.
Both definitions of the Intvektor class and the definition of the Matris
class can be seen at the bottom.
I wrote an overloaded indexoperator in the Intvektor class but that one
was very easy that you can see in the class definition of Intvektor.

(snip)

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
a[0][0] = 1; //I want this to work
return 0;
}

(snip)

Check out the C++ FAQ (http://www.parashift.com/c++-faq-lite/) it talks
about this specific thing. Basically, you over the operator() so it looks
like operator()(int r, int c). So you would have this:

a(0, 0) = 1;
--
Alvin
Jul 23 '05 #2
Tony Johansson wrote:
Hello Experts!!

I have two small classes called Intvektor and Matris shown at the bottom and
a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns.
This works fine! So that's not the problem.
I just mentioned it here just to get a better understand of the whole
problem

Because I have dynamically allocated memory I must delete all these
dynamically allocated memory.
The constructor of Matris where I allocate dynamically works fine.
The destructor where I delete all the allocated memory also works fine.

Now to my problem I want to be able to use an overloaded indexoperator on my
matris object.
In the main program I want to be able to write a[0][0] = 1; meaning putting
the number 1
in row number index 0 and column number index 0. The object a is an instance
of class Matris.
Both definitions of the Intvektor class and the definition of the Matris
class can be seen at the bottom.
I wrote an overloaded indexoperator in the Intvektor class but that one was
very easy that you can see in the class definition of Intvektor.

So have you any idea how to write an overloaded indexoperator for the Matris
class?
I have no idea so I try to ask you out there.

Many thanks
//Tony
//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
a[0][0] = 1; //I want this to work
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk) //default Constructor and user
defined constructor
{ array = new int[size]; }
~Intvektor() //Destruktor
{ delete array; }

Intvektor(const Intvektor& v) //Copy konstruktor
{ copy(v); }

int operator[](int i) const //oveloaded indexoperator
{ return array[i]; }

int& operator[](int i) //oveloaded indexoperator
{ return array[i]; }

const Intvektor& operator=(const Intvektor& v) //assignment operator
{
if (this != &v)
{
delete []array;
copy(v);
}
return *this;
}

private:

void Intvektor::copy(const Intvektor& v) //is called from both
copy constructor and assignment operator
{
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}

int size;
int* array;
};

//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{ matris = new Intvektor*[0]; }//default constructor

Matris(int rows, int cols) : c_rows(rows), c_columns(cols) //user
defined Constructor
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris[i] = new Intvektor[c_columns];
}

Matris(const Matris& v) //Copy constructor
{ copy(v) //is located in the private section }

~Matris() //Destruktor
{ remove(); //Is located in the private section }

const Matris& operator=(const Matris& v) // assignment operator
{
if (this != &v)
{
remove();
copy(v);
}
return *this;
}
private:

void copy(const Matris& v) // is called from both copy constructor
and assignemt operator
{
c_rader = v.c_rader;
c_kolumner = v.c_kolumner;
matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}

void remove() // is called from both destructor and assignment
operator
{
for (int i=0; i< c_rader; i++)
delete[] matris[i];
delete[] matris;
}

int c_rows, c_columns;
Intvektor **matris;
};


Just use 'std::vector' and most of the work/code is done
for you. Here's a simple example:

---- Matrix.h ----

// Matrix.h
#include <vector>
#include <iostream>

// a vector of int's
typedef std::vector< int > Intvektor;

// a vector of Intvektor's (i.e. a 2 dim matrix of int's)
typedef std::vector< Intvektor > Int2vektor;
// class Matrix derived from a vector of vectors of int.
// all of the features & methods of 'vector' are available to Matrix.
class Matrix : public Int2vektor
{
public:
Matrix() : Int2vektor()
{
}

Matrix(int row, int col) : Int2vektor(row, Intvektor(col))
{
}

Matrix(const Matrix& m) : Int2vektor(m)
{
}

// dump my contents to 'os' for debugging
std::ostream& dump(std::ostream& os, const char * title = NULL)
{
if (title)
os << title << std::endl;

for (int row = 0; row < size(); row++)
{
os << "row: " << row << std::endl;
for (int col = 0; col < this->operator[](row).size(); col++)
os << " " << this->operator[](row)[col];
os << std::endl;
}

os << std::endl;

return os;
}
};

---- end of Matrix.h ----

---- MatrixTest.cpp ----

// Matrix.cpp
#include "Matrix.h"

int main()
{
Matrix a(2,2);

// dump the initial contents of 'a'
a.dump(std::cout, "'a' initial contents");

int v = 0;

// put some values into 'a'
for (int row = 0; row < a.size(); row++)
for (int col = 0; col < a[row].size(); col++)
a[row][col] = v++;

// dump the new contents of 'a'
a.dump(std::cout, "'a' with values added");

// make 'b' as a copy of 'a'
Matrix b(a);

// dump the contents of 'b'
b.dump(std::cout, "'b' as a copy of 'a'");

// erase 'a'
a.clear();

// demonstrate that the copy-constructor
// actually copied the data from 'a' to 'b'
a.dump(std::cout, "'a' after a.clear()");
b.dump(std::cout, "'b' after a.clear()");

// copy 'b' to 'a'
a = b;
a.dump(std::cout, "'a' after 'a = b'");

return 0;
}

---- end of MatrixTest.cpp ----

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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. ...
5
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: ...
1
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...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
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 ?
2
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...
5
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...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.