473,751 Members | 5,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing operator functions

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::DatAr ray(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::~DatA rray() {
int c1;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;
}

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

void DatArray::opera tor = (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::opera tor ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
}

/*DatArray DatArray::opera tor + (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

Mar 14 '07 #1
8 2486
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 :)
Mar 14 '07 #2
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
Mar 14 '07 #3
On Mar 14, 2:05 pm, "valerij" <valerij.rozou. ..@gmail.comwro te:
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

Mar 14 '07 #4
On 14 Mar 2007 11:05:35 -0700 in comp.lang.c++, "valerij"
<va************ **@gmail.comwro te,
>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::DatA rray(int r, int c) {
DatArray::DatAr ray(int r, int c)
: a(r, std::vector<dou ble>(c))
{ }
>/*DatArray DatArray::opera tor + (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::opera tor += (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;
}

Mar 14 '07 #5
On Mar 14, 1:05 pm, "valerij" <valerij.rozou. ..@gmail.comwro te:
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::DatAr ray(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::~DatA rray() {
int c1;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;

}

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

}

void DatArray::opera tor = (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::opera tor ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;

}

/*DatArray DatArray::opera tor + (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::DatAr ray(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::~DatA rray() {
int c1;
cout << "Destroying " << name << endl;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;
}

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

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

void DatArray::opera tor = (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::opera tor ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
}

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

DatArray* DatArray::opera tor + (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::opera tor - (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::opera tor * (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::opera tor / (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

Mar 14 '07 #6
On Mar 14, 1:22 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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

Mar 14 '07 #7
On Mar 14, 1:58 pm, David Harmon <sou...@netcom. comwrote:
On 14 Mar 2007 11:05:35 -0700 in comp.lang.c++, "valerij"
<valerij.rozou. ..@gmail.comwro te,
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::DatAr ray(int r, int c) {

DatArray::DatAr ray(int r, int c)
: a(r, std::vector<dou ble>(c))
{ }
/*DatArray DatArray::opera tor + (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::opera tor += (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 ;)

Mar 14 '07 #8
On 14 Mar 2007 12:50:26 -0700 in comp.lang.c++, "valerij"
<va************ **@gmail.comwro te,
>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

Mar 15 '07 #9

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

Similar topics

17
1507
by: Bart Nessux | last post by:
I understand that most people write functions to reuse code, no? So, I assume it would be better to write functions that are very specific, as opposed to those that are more generic. However, I have difficulty doing this. My code doesn't need to be super-modular (I don't need functions that can be used in dozens of different programs). So, my functions don't tend to be portbale and can sometimes span one, or perhaps two pages. Is this...
2
18569
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated since...
7
2092
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
4
2904
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
4
2083
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
3
1592
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends; // here x contains "004400C8pluto"
1
2173
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const char*:" << str << '\n'; } void print(int x)
8
3249
by: CodeCracker | last post by:
Is it possible to write such an operator function? watch that there is a space in between unsigned and long. Does this mean I have to write two operator function one for long and another for unsigned? Regards,
89
3844
by: Skybuck Flying | last post by:
Hello, This morning I had an idea how to write Scalable Software in general. Unfortunately with Delphi 2007 it can't be done because it does not support operating overloading for classes, or record inheritance (records do have operator overloading) The idea is to write a generic integer class with derived integer classess for 8 bit, 16 bit, 32 bit, 64 bit and 64 bit emulated.
0
9009
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
9591
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9349
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,...
0
8269
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6819
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
6090
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
4721
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...
2
2815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.