473,320 Members | 1,936 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,320 software developers and data experts.

Operator function for object pointers

I define a class "class xxx" with the following operator override:

class xxx {
int xxx::operator==(const xxx &ptr) const;
}

So if I define

xxx A;
xxx B;

I can do stuff like

if (A == B) {};

My question is what if instead I have

xxx *A_ptr = new xxx;
xxx *B_ptr = new xxx;

How do I define the operator override to work on object pointers
instead of by reference.

if (A_Ptr == B_ptr) {}

Jun 25 '07 #1
3 1272
kenkahn wrote:
I define a class "class xxx" with the following operator override:

class xxx {
int xxx::operator==(const xxx &ptr) const;
}

So if I define

xxx A;
xxx B;

I can do stuff like

if (A == B) {};

My question is what if instead I have

xxx *A_ptr = new xxx;
xxx *B_ptr = new xxx;

How do I define the operator override to work on object pointers
instead of by reference.

if (A_Ptr == B_ptr) {}
You cannot redefine operators for built-in types, and pointers are
built-in types.

Do
if (A_Ptr && B_Ptr && *A_Ptr == *B_ptr) {}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 25 '07 #2
On Mon, 25 Jun 2007 14:07:11 -0700, kenkahn <ke*****@optonline.netwrote:
I define a class "class xxx" with the following operator override:

class xxx {
int xxx::operator==(const xxx &ptr) const;
}
Two errors and a very unfortunate name in three lines. You mean

class xxx {
// public and so on ...
bool operator== (const xxx& other) const;
};

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jun 25 '07 #3
On Jun 25, 11:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
kenkahn wrote:
[...]
My question is what if instead I have
xxx *A_ptr = new xxx;
xxx *B_ptr = new xxx;
How do I define the operator override to work on object pointers
instead of by reference.
if (A_Ptr == B_ptr) {}
You cannot redefine operators for built-in types, and pointers are
built-in types.
The important point, of course, is that the above expression
already has a defined semantics. With very few exceptions
(unary operator&), you can't change defined semantics.
Do
if (A_Ptr && B_Ptr && *A_Ptr == *B_ptr) {}
if ( A_Ptr == NULL
? B_Ptr == NULL
: B_Ptr != NULL && *A_Ptr == *B_Ptr ) {}

:-)

Most of the time, I think I'd go with a function call.

(PS: I'm not saying that your response was wrong. Since he
didn't say what the semantics were supposed to be, we don't
know. Most of the time I'm dealing with pointers, the built-in
semantics are what I want.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 26 '07 #4

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

Similar topics

3
by: glen stark | last post by:
Hi all. I'm working with an array of member function pointers (they are all get function of the class Bead). The typedef is: typedef _real (Bead::*_beadGfp)(void); I have a class System...
2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
2
by: Tony Johansson | last post by:
Hello Experts! I have one class template here called Handle and one concrete class called Point. At the bottom is the main program and the class template definitions for Handle. This program...
24
by: Bangalore | last post by:
Hi all, I have a problem in accessing elements using overloaded operator . Consider, const int SIZE=10; int FALSE=0; class Array { private: int x; public:
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
4
by: Abubakar | last post by:
Hi all, I downloaded a code (a c++ project) over the internet and at one place I saw the following lline: // allow usage as a pointer. operator CMemDC*() { return this; } (the comment up...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
6
by: shsingh | last post by:
I have a function which takes two operands (char*) and a operator (char*) int myFunc(char* operand1, char* operand2, char* myoperator); Depending upon the operator, i want to do the...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.