How do I write an overloaded indexoperator

T

Tony Johansson

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; }

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

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 = v.array;
}

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 = 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 = new Intvektor[c_kolumner];
}

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

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

Alvin

Tony said:
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;
 
L

Larry I Smith

Tony said:
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; }

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

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 = v.array;
}

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 = 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 = new Intvektor[c_kolumner];
}

void remove() // is called from both destructor and assignment
operator
{
for (int i=0; i< c_rader; i++)
delete[] matris;
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::eek:stream& dump(std::eek:stream& 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top