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

Question about Operator overloading

Hi,

I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.

Now I have a class xydata with operator- overloaded:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
}
}

For some reason, this results in a core dump, when I later do:
xydata a(...);
xydata b(...);
xydata c = a - b;
In contrast, when I return a pointer from the operator rather than an
object, everything works as expected:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata* operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata *res = new xydata(xr, yr, len);
return res;
}
}

// later
xydata a(...);
xydata b(...);
xydata *c = a - b;

In the book I mentioned above, I read that it should be possible to
return objects from overloaded operators. Did I miss something?

Thanks much in advance for a short clarification...
Eckhard
Nov 2 '05 #1
7 1802
Eckhard Lehmann wrote:
Hi,

I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.

Now I have a class xydata with operator- overloaded:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata operator-(xydata subtr) {
xydata operator-(const xydata &subtr)

You don't want to copy subtr, nor do you want to modify it.
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
}
This should look like (semantically, of course):

X val1 = this->val1 - subtr.val1;
X val2 = this->val2 - subtr.val2;

return xydata(val1, val2);
}

For some reason, this results in a core dump, when I later do:
xydata a(...);
xydata b(...);
xydata c = a - b;
Looks to me that xydata doesn't have proper copy constructor and
destructor (and probably assignement operator). Without seeing the rest
of the class, just make sure xydata correctly copies and deletes the xr
and yr values you give it.
In contrast, when I return a pointer from the operator rather than an
object, everything works as expected:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata* operator-(xydata subtr) {
Don't do that.
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata *res = new xydata(xr, yr, len);
return res;
}
}

// later
xydata a(...);
xydata b(...);
xydata *c = a - b;
That's probably a memory leak, and it won't work as expected :

xydata a, b, c;
xydata *d = a - b - c; // error
In the book I mentioned above, I read that it should be possible to
return objects from overloaded operators. Did I miss something?


The problem is not with operator overloading, but with memory
management. Does

int main()
{
xydata a(..);
xydata b(a);
}

work correctly?
Jonathan

Nov 2 '05 #2
On Wed, 02 Nov 2005 19:10:20 +0100, Eckhard Lehmann <ec****@web.de>
wrote:
Hi,

I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.

Now I have a class xydata with operator- overloaded:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
}
}

For some reason, this results in a core dump, when I later do:
xydata a(...);
xydata b(...);
xydata c = a - b;
In contrast, when I return a pointer from the operator rather than an
object, everything works as expected:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata* operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata *res = new xydata(xr, yr, len);
return res;
}
}

// later
xydata a(...);
xydata b(...);
xydata *c = a - b;

In the book I mentioned above, I read that it should be possible to
return objects from overloaded operators. Did I miss something?


Usually one would implement operator-() not as a member function, but
as a non-member function and define it in terms of operator-=() which
would be a member function. The same goes for operator+(). Both the
non-member operator+() and operator-() would indeed return an object.

However, it seems that you have much more serious problems, namely
memory management ... you leak lots of memory because you call
operator new (and operator new[]) three times within the function, yet
there is no delete (nor delete[]) in sight???

Unfortunately, I don't know this book ... but if the sample code looks
anything like this, I would get a new book!

--
Bob Hairgrove
No**********@Home.com
Nov 2 '05 #3
Eckhard Lehmann wrote:
Hi,

I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.

Now I have a class xydata with operator- overloaded:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata operator-(xydata subtr) {
Does your book really tell you to write an overloaded operator like that?
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
Why not

return xydata(xr, yr, len);
}
}

For some reason, this results in a core dump, when I later do:
xydata a(...);
xydata b(...);
xydata c = a - b;
In contrast, when I return a pointer from the operator rather than an
object, everything works as expected:

Probably the lack of a valid copy constructor.

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata* operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata *res = new xydata(xr, yr, len);
return res;
}
}

// later
xydata a(...);
xydata b(...);
xydata *c = a - b;

In the book I mentioned above, I read that it should be possible to
return objects from overloaded operators. Did I miss something?
It's not just valid, it's the only sensible way to do it.

Thanks much in advance for a short clarification...

Does your class have a valid copy constructor, does it have a copy
constructor at all? Does you bible talk about copy constructors and
assignment operators and why they are important?

Here's a test, go back to the non-pointer version of your operator- but
this time remove the destructor from your class. If this time you don't
get a crash then the problem is the lack of a copy constructor.

Post again if your bible doesn't cover copy constructors.

Eckhard


john
Nov 2 '05 #4
lpw
"Eckhard Lehmann" <ec****@web.de> wrote in message
news:43***********************@news.freenet.de...
Hi,

I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.

Now I have a class xydata with operator- overloaded:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
}
}

For some reason, this results in a core dump, when I later do:
xydata a(...);
xydata b(...);
xydata c = a - b;
In contrast, when I return a pointer from the operator rather than an
object, everything works as expected:

class xydata {
public:
xydata(double *x, double *y, int len)
...
xydata* operator-(xydata subtr) {
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata *res = new xydata(xr, yr, len);
return res;
}
}

// later
xydata a(...);
xydata b(...);
xydata *c = a - b;

In the book I mentioned above, I read that it should be possible to
return objects from overloaded operators. Did I miss something?

Thanks much in advance for a short clarification...
Eckhard

I would like to reitarate what others have said before: implement a good
copy contructor. Most importantly, make sure that your copy constructor
does a deep copy of the argument. Otherwise, since you seem to use
dynamically allocated members, deleting some object A will invalidate all
other objects from which A has been copied or for which A served as the
source copy. The same goes for the assignment operator, should you choose
to implement one as well.

Nov 2 '05 #5
> However, it seems that you have much more serious problems, namely
memory management ... you leak lots of memory because you call
operator new (and operator new[]) three times within the function, yet
there is no delete (nor delete[]) in sight???
I consider memory management - I just posted some code fragments that I
took for important to explain the problem.
Unfortunately, I don't know this book ... but if the sample code looks
anything like this, I would get a new book!


Nono.. this is a real world problem, not sample code ;-)!
The sample code in the book talks about a "Date" class and
"Employers/Persons", like computer books usually do. I haven't found
any errors in the sample code yet - at least not in the few passages
that I retyped.

Eckhard

Nov 3 '05 #6
> Does your book really tell you to write an overloaded operator like that?

No, but the book unfortunately gives only an example in which an int is
added to an object, not an object to another object. This is too simple
to cover all possibilities.
int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
Why not

return xydata(xr, yr, len);


Between the construction of res and returning of res, the xr and yr
arrays are delete[]'d. I left that out here, because I didn't think
that it is important.
Does your class have a valid copy constructor, does it have a copy
constructor at all? Does you bible talk about copy constructors and
assignment operators and why they are important?


The book has a chapter on that, of course - I just didn't read it,
because I didn't think that it has something to do with operator
overloading.
Ok, after some reading and try & error - and the awesome and surprising
experience that it is obviously possible to access protected and
private members of a different object in an object method (??) - I have
a valid copy constructor:

xydata::xydata (const xydata& other)
{
this->_len = other._len; // _len is private - why can I acces it
here? strange

this->_data = new double[2 * this->_nPoints];
for (int i = 0; i < 2 * this->_nPoints; i++) {
this->_data[i] = other._data[i]; //the same here
}
}

Are objects of a class friends by default - or how can I explain this?
Anyway, thank you all for the help, I learned very much from this
diskussion..

Eckhard

Nov 3 '05 #7
ec****@web.de wrote:
Does your book really tell you to write an overloaded operator like that?

No, but the book unfortunately gives only an example in which an int is
added to an object, not an object to another object. This is too simple
to cover all possibilities.


The FAQ is quite good on this

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

Point 7 is the one that your code ignores (as do many other programmers).

int len = subtr.len();
double *xr = new double[len], *yr = new double[len];
// subract subtr from *this
...
xydata res(xr, yr, len);
return res;
Why not

return xydata(xr, yr, len);

Between the construction of res and returning of res, the xr and yr
arrays are delete[]'d. I left that out here, because I didn't think
that it is important.

Does your class have a valid copy constructor, does it have a copy
constructor at all? Does you bible talk about copy constructors and
assignment operators and why they are important?

The book has a chapter on that, of course - I just didn't read it,
because I didn't think that it has something to do with operator
overloading.
Ok, after some reading and try & error - and the awesome and surprising
experience that it is obviously possible to access protected and
private members of a different object in an object method (??) - I have
a valid copy constructor:

xydata::xydata (const xydata& other)
{
this->_len = other._len; // _len is private - why can I acces it
here? strange


It's perfectly logical when you understand the purpose of
public/private. It's to protect _code_ outside of the _class_ from
becoming dependent on the implementation details of the class. It's
nothing to with objects.

this->_data = new double[2 * this->_nPoints];
for (int i = 0; i < 2 * this->_nPoints; i++) {
this->_data[i] = other._data[i]; //the same here
}
}

Are objects of a class friends by default - or how can I explain this?


Friendship is something that is granted on a class by class basis, not
an object by object basis. So it makes no sense to say that objects of a
class are friends. But I get what you are saying, and yes it's true. If
this surprises you then you have misundetood the purpose of
public/private. Better look up the term 'encapsulation'.

john
Nov 3 '05 #8

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

Similar topics

7
by: Jessica | last post by:
Hi, I have a design question. I am making a time series analysis tool. Since I already use STL vector to represent time series, is there a need to implement a class for the time series object? ...
11
by: billnospam | last post by:
Is it possible to overload operators in vb.net? Is it possible to do programmer defined boxing on byvalue variables in vb.net?
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
110
by: Vijay Kumar R Zanvar | last post by:
Hi, Which section of C99 says that return value of malloc(3) should not be casted? Thanks. -- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
5
by: Joseph Y. Oh | last post by:
Hello, class A { public: A& operator+=( const A& a ) = 0; }; class B1:public A { public: A& operator+=( const A& a ) { ... }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.