473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1687
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::ostre am& 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::cou t, "'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::cou t, "'a' with values added");

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

// dump the contents of 'b'
b.dump(std::cou t, "'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::cou t, "'a' after a.clear()");
b.dump(std::cou t, "'b' after a.clear()");

// copy 'b' to 'a'
a = b;
a.dump(std::cou t, "'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
17877
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
5
6886
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: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
1
2225
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 pertaining to casting class A to class B, the implementation of the
4
1620
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 Edition that I have posted to comp.sources.d). How do we decide which is more appropriate? Why are the overloaded "<<" and ">>" operators always friends? Also, what is an appropriate application for the overloaded function call operator?
44
2425
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 statement: void print_structs(void * struct_argument, enum struct_type stype) { switch(stype) { case STRUCT_TYPE_1:
3
2254
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
7375
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 number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
5
2293
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
2
3889
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
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.