Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Writing operator functions

Reply
Thread Tools

Writing operator functions

 
 
valerij
Guest
Posts: n/a
 
      03-14-2007
Yes, hi

How to write "operator +" and "operator =" functions in a class with
a defined constructor? The following code demonstrates that I don't
really understand how to do it... I think it has something to do with
the compiler calling the destructor twice. Could someone point out
where I go wrong?
P.S.: The error it gives is "Debug Assertion Failure ....." (at run
time)
P.P.S: Everything else works just fine (without the use of "operator
+"
and "operator =" functions)

//code "tested" on Microsoft Visual C++ 6.0
#include <iostream>

using namespace std;

class DatArray {
public:
int rows, columns;
double **a;
DatArray(int r, int c);
~DatArray();
void operator = (double d1);
void operator = (DatArray da1); //problematic
void operator ++ ();
//DatArray operator + (double d1); //does not work
};

DatArray:atArray(int r, int c) {
int c1;
rows = r;
columns = c;
a = new double* [rows];
for (c1 = 0; c1 < rows; c1++) a[c1] = new double [columns];
}

DatArray::~DatArray() {
int c1;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;
}

void DatArray:perator = (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = d1;
}

void DatArray:perator = (DatArray da1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = da1.a[c1][c2];
}

void DatArray:perator ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
}

/*DatArray DatArray:perator + (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = a[c1][c2] + d1;
return *this;
}*/

int main() {
int c1, c2;
DatArray myarray(3, 4), myarray2(3, 4);
myarray = 10.1;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray.a[c1][c2]
<< " ";
cout << endl;
}
cout << endl;
for (c1 = 0; c1 < 100; c1++) myarray++;
//myarray2 = myarray + 100; //this should be simpler, but does not
work ?!?!?!?!?!
myarray2 = myarray;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray2.a[c1][c2]
<< " ";
cout << endl;
}
cin.get(); //currently, after this, ERROR ... WHY?!?!?!?!?! (how to
fix this?!?!?!?!?)
return 0;
}
//end of code "tested" on Microsoft Visual C++ 6.0

Thanks,
Valerij

 
Reply With Quote
 
 
 
 
Piyo
Guest
Posts: n/a
 
      03-14-2007
valerij wrote:
> Yes, hi
>
> How to write "operator +" and "operator =" functions in a class with
> a defined constructor? The following code demonstrates that I don't
> really understand how to do it... I think it has something to do with
> the compiler calling the destructor twice. Could someone point out
> where I go wrong?
> P.S.: The error it gives is "Debug Assertion Failure ....." (at run
> time)
> P.P.S: Everything else works just fine (without the use of "operator
> +"
> and "operator =" functions)
>
> //code "tested" on Microsoft Visual C++ 6.0
> #include <iostream>
>
> using namespace std;
>
> class DatArray {
> public:
> int rows, columns;
> double **a;
> DatArray(int r, int c);
> ~DatArray();
> void operator = (double d1);
> void operator = (DatArray da1); //problematic
> void operator ++ ();
> //DatArray operator + (double d1); //does not work
> };
>


My gut instinct tells me you are missing a Copy Constructor.
I'd have to look at it closely to tell later
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2007
valerij wrote:
> Yes, hi
>
> How to write "operator +" and "operator =" functions in a class with
> a defined constructor? The following code demonstrates that I don't
> really understand how to do it... I think it has something to do with
> the compiler calling the destructor twice. Could someone point out
> where I go wrong?
> P.S.: The error it gives is "Debug Assertion Failure ....." (at run
> time)
> P.P.S: Everything else works just fine (without the use of "operator
> +"
> and "operator =" functions)
>
> //code "tested" on Microsoft Visual C++ 6.0
> #include <iostream>
>
> using namespace std;
>
> class DatArray {
> public:
> int rows, columns;
> double **a;
> DatArray(int r, int c);
> ~DatArray();
> void operator = (double d1);
> void operator = (DatArray da1); //problematic



You've violated the "Rule of Three". Look it up. Also, consider
that operator = (the assignment op) should probably take a ref to
a const object instead of an object.

> void operator ++ ();
> //DatArray operator + (double d1); //does not work
> };
> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 2:05 pm, "valerij" <(E-Mail Removed)> wrote:
> How to write "operator +" and "operator =" functions in a class with
> a defined constructor?

[snip]

Read FAQs 16.16 thru 16.19. They present several ways to create a 2-D
array (i.e., at Matrix) class:

http://www.parashift.com/c++-faq-lit...html#faq-16.16

Cheers! --M

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      03-14-2007
On 14 Mar 2007 11:05:35 -0700 in comp.lang.c++, "valerij"
<(E-Mail Removed)> wrote,
>How to write "operator +" and "operator =" functions in a class with
>a defined constructor?


operator+ should usually be

T operator+(T const & t1, T const & t2)
{
T result(t1);
result += t2;
return result;
}

operator= depends on the requirements of your class, but usually
you just use operator= of each of the base classes and members.
(That is the compiler supplied operator= if you do not write one,
but unfortunately you have to write += yourself.)

operator+= is a lot like operator= except using += instead of =.
All the = operators (if you need them) should be members.

>//code "tested" on Microsoft Visual C++ 6.0


Failure to upgrade to at least MSVC 7.x may be harmful to your sanity.

>class DatArray {
>public:
> int rows, columns;
> double **a;


std::vector<std::vector<double> > a;


>DatArray:atArray(int r, int c) {


DatArray:atArray(int r, int c)
: a(r, std::vector<double>(c))
{ }

>/*DatArray DatArray:perator + (double d1) {
> int c1, c2;
> for (c1 = 0; c1 < rows; c1++)
> for (c2 = 0; c2 < columns; c2++) a[c1][c2] = a[c1][c2] + d1;
> return *this;
>}*/


DatArray & DatArray:perator += (double d1) {
for (int c1 = 0; c1 < a.size(); c1++)
for (int c2 = 0; c2 < a[c1].size(); c2++)
a[c1][c2] += d1;
return *this;
}

 
Reply With Quote
 
valerij
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 1:05 pm, "valerij" <(E-Mail Removed)> wrote:
> Yes, hi
>
> How to write "operator +" and "operator =" functions in a class with
> a defined constructor? The following code demonstrates that I don't
> really understand how to do it... I think it has something to do with
> the compiler calling the destructor twice. Could someone point out
> where I go wrong?
> P.S.: The error it gives is "Debug Assertion Failure ....." (at run
> time)
> P.P.S: Everything else works just fine (without the use of "operator
> +"
> and "operator =" functions)
>
> //code "tested" on Microsoft Visual C++ 6.0
> #include <iostream>
>
> using namespace std;
>
> class DatArray {
> public:
> int rows, columns;
> double **a;
> DatArray(int r, int c);
> ~DatArray();
> void operator = (double d1);
> void operator = (DatArray da1); //problematic
> void operator ++ ();
> //DatArray operator + (double d1); //does not work
>
> };
>
> DatArray:atArray(int r, int c) {
> int c1;
> rows = r;
> columns = c;
> a = new double* [rows];
> for (c1 = 0; c1 < rows; c1++) a[c1] = new double [columns];
>
> }
>
> DatArray::~DatArray() {
> int c1;
> for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
> delete [] a;
>
> }
>
> void DatArray:perator = (double d1) {
> int c1, c2;
> for (c1 = 0; c1 < rows; c1++)
> for (c2 = 0; c2 < columns; c2++) a[c1][c2] = d1;
>
> }
>
> void DatArray:perator = (DatArray da1) {
> int c1, c2;
> for (c1 = 0; c1 < rows; c1++)
> for (c2 = 0; c2 < columns; c2++) a[c1][c2] = da1.a[c1][c2];
>
> }
>
> void DatArray:perator ++ () {
> int c1, c2;
> for (c1 = 0; c1 < rows; c1++)
> for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
>
> }
>
> /*DatArray DatArray:perator + (double d1) {
> int c1, c2;
> for (c1 = 0; c1 < rows; c1++)
> for (c2 = 0; c2 < columns; c2++) a[c1][c2] = a[c1][c2] + d1;
> return *this;
>
> }*/
>
> int main() {
> int c1, c2;
> DatArray myarray(3, 4), myarray2(3, 4);
> myarray = 10.1;
> for (c1 = 0; c1 < myarray.rows; c1++) {
> for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray.a[c1][c2]
> << " ";
> cout << endl;
> }
> cout << endl;
> for (c1 = 0; c1 < 100; c1++) myarray++;
> //myarray2 = myarray + 100; //this should be simpler, but does not
> work ?!?!?!?!?!
> myarray2 = myarray;
> for (c1 = 0; c1 < myarray.rows; c1++) {
> for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray2.a[c1][c2]
> << " ";
> cout << endl;
> }
> cin.get(); //currently, after this, ERROR ... WHY?!?!?!?!?! (how to
> fix this?!?!?!?!?)
> return 0;}
>
> //end of code "tested" on Microsoft Visual C++ 6.0
>
> Thanks,
> Valerij


Hi again,

I seem to have figured it out. You have to use pointers. The working
code is below. My next question is how to implement the following:

DatArray* operator + (DatArray da1);

Thanks,
Valerij

//code tested on Microsoft Visual C++ 6.0
#include <iostream>

using namespace std;

class DatArray {
public:
int rows, columns;
int name;
double **a;
DatArray(int r, int c);
~DatArray();
void operator = (double d1);
void operator = (int i1);
void operator = (DatArray* da1);
DatArray* operator + (double d1);
DatArray* operator - (double d1);
DatArray* operator * (double d1);
DatArray* operator / (double d1);
void operator ++ ();
void operator -- ();
};

DatArray:atArray(int r, int c) {
int c1;
rows = r;
columns = c;
a = new double* [rows];
for (c1 = 0; c1 < rows; c1++) a[c1] = new double [columns];
name = 0;
}

DatArray::~DatArray() {
int c1;
cout << "Destroying " << name << endl;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;
}

void DatArray:perator = (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = d1;
}

void DatArray:perator = (int i1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = i1;
}

void DatArray:perator = (DatArray* da1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = da1->a[c1][c2];
}

void DatArray:perator ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
}

void DatArray:perator -- () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]--;
}

DatArray* DatArray:perator + (double d1) {
int c1, c2;
DatArray *da1;
da1 = new DatArray(rows, columns);
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) da1->a[c1][c2] = a[c1][c2] + d1;
return da1;
}

DatArray* DatArray:perator - (double d1) {
int c1, c2;
DatArray *da1;
da1 = new DatArray(rows, columns);
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) da1->a[c1][c2] = a[c1][c2] - d1;
return da1;
}

DatArray* DatArray:perator * (double d1) {
int c1, c2;
DatArray *da1;
da1 = new DatArray(rows, columns);
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) da1->a[c1][c2] = a[c1][c2] * d1;
return da1;
}

DatArray* DatArray:perator / (double d1) {
int c1, c2;
DatArray *da1;
da1 = new DatArray(rows, columns);
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) da1->a[c1][c2] = a[c1][c2] / d1;
return da1;
}

int main() {
int c1, c2;
DatArray myarray(3, 4), myarray2(3, 4);

myarray.name = 1;
myarray2.name = 2;

myarray = 0;
myarray2 = 300;
myarray = myarray2 + 1000;
myarray2 = myarray / 100;

for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray.a[c1][c2]
<< " ";
cout << endl;
}
cout << endl;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray2.a[c1][c2]
<< " ";
cout << endl;
}

return 0;
}
//end of code tested on Microsoft Visual C++ 6.0

 
Reply With Quote
 
valerij
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 1:22 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> valerij wrote:
> > Yes, hi

>
> > How to write "operator +" and "operator =" functions in a class with
> > a defined constructor? The following code demonstrates that I don't
> > really understand how to do it... I think it has something to do with
> > the compiler calling the destructor twice. Could someone point out
> > where I go wrong?
> > P.S.: The error it gives is "Debug Assertion Failure ....." (at run
> > time)
> > P.P.S: Everything else works just fine (without the use of "operator
> > +"
> > and "operator =" functions)

>
> > //code "tested" on Microsoft Visual C++ 6.0
> > #include <iostream>

>
> > using namespace std;

>
> > class DatArray {
> > public:
> > int rows, columns;
> > double **a;
> > DatArray(int r, int c);
> > ~DatArray();
> > void operator = (double d1);
> > void operator = (DatArray da1); //problematic

>
> You've violated the "Rule of Three". Look it up. Also, consider
> that operator = (the assignment op) should probably take a ref to
> a const object instead of an object.
>
> > void operator ++ ();
> > //DatArray operator + (double d1); //does not work
> > };
> > [..]

>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


OMG. I never heard of it ... just looking at it now ... this might
just be what i always wanted to know ...
thanks

 
Reply With Quote
 
valerij
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 1:58 pm, David Harmon <(E-Mail Removed)> wrote:
> On 14 Mar 2007 11:05:35 -0700 in comp.lang.c++, "valerij"
> <(E-Mail Removed)> wrote,
>
> >How to write "operator +" and "operator =" functions in a class with
> >a defined constructor?

>
> operator+ should usually be
>
> T operator+(T const & t1, T const & t2)
> {
> T result(t1);
> result += t2;
> return result;
> }
>
> operator= depends on the requirements of your class, but usually
> you just use operator= of each of the base classes and members.
> (That is the compiler supplied operator= if you do not write one,
> but unfortunately you have to write += yourself.)
>
> operator+= is a lot like operator= except using += instead of =.
> All the = operators (if you need them) should be members.
>
> >//code "tested" on Microsoft Visual C++ 6.0

>
> Failure to upgrade to at least MSVC 7.x may be harmful to your sanity.
>
> >class DatArray {
> >public:
> > int rows, columns;
> > double **a;

>
> std::vector<std::vector<double> > a;
>
> >DatArray:atArray(int r, int c) {

>
> DatArray:atArray(int r, int c)
> : a(r, std::vector<double>(c))
> { }
>
> >/*DatArray DatArray:perator + (double d1) {
> > int c1, c2;
> > for (c1 = 0; c1 < rows; c1++)
> > for (c2 = 0; c2 < columns; c2++) a[c1][c2] = a[c1][c2] + d1;
> > return *this;
> >}*/

>
> DatArray & DatArray:perator += (double d1) {
> for (int c1 = 0; c1 < a.size(); c1++)
> for (int c2 = 0; c2 < a[c1].size(); c2++)
> a[c1][c2] += d1;
> return *this;
>
> }


You see, I have Windows 98SE, so any higher and it will be just too
slow

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      03-15-2007
On 14 Mar 2007 12:50:26 -0700 in comp.lang.c++, "valerij"
<(E-Mail Removed)> wrote,
>You see, I have Windows 98SE, so any higher and it will be just too
>slow


OK, that's a side issue. Don't neglect the part about getting rid of
the raw pointers and raw arrays. Prefer std::vector in application
level code.

Digital Mars C++ is very efficient under '98, and far more standard-
conforming than VC6. http://digitalmars.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing MyClass::Operator unsigned long() CodeCracker C++ 8 06-09-2006 02:11 PM
operator[] and different behaviour for reading and writing Mark Stijnman C++ 4 05-12-2005 03:52 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Re: Writing an 'operator=' Jonathan de Boyne Pollard C++ 0 08-10-2004 04:44 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments