473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pointer arthrmetic using operator overloading?

Hello All,
Can you please let me know if we can do pointer arthrmetic using
operator overloading? If not, can you please explain why it's not
supported by compiler?

I tried below e.g. which was giving me error.

typedef class x
{
}X;
void operator +(X *p,X *q)
{
}
main()
{
X *p,*q;
p+q;
}

[/home/dbk/temp_eg]: gcc ptr.cpp
ptr.cpp:5: `operator +(X *, X *)' must have an argument of class or
enumerated type
ptr.cpp: In function `int main()':
ptr.cpp:10: invalid operands `X *' and `X *' to binary `operator +'
Any idea what are these errors?

TIA,
Bharath

Jun 12 '07 #1
2 3162
Bharath wrote:
Hello All,
Can you please let me know if we can do pointer arthrmetic using
operator overloading? If not, can you please explain why it's not
supported by compiler?
You can only overload operators for user defined types (i.e. classes).
Pointers are predefined types.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 12 '07 #2
Bharath wrote:
Hello All,
Can you please let me know if we can do pointer arthrmetic using
operator overloading?
Yes. However, you cannot replace the built-in functionality for pointers
with a user-defined operator.
If not, can you please explain why it's not supported by compiler?
The idea of operator overloading is that you can extend the language, not
replace the existing functionality with different behavior. So you cannot
write yur own operators for built-in types, but only for user-defined ones.
I tried below e.g. which was giving me error.

typedef class x
{
}X;
void operator +(X *p,X *q)
{
}
main()
{
X *p,*q;
p+q;
}

[/home/dbk/temp_eg]: gcc ptr.cpp
ptr.cpp:5: `operator +(X *, X *)' must have an argument of class or
enumerated type
An operator must have at least one argument of user-defined type, and
pointers are built-in types.
ptr.cpp: In function `int main()':
ptr.cpp:10: invalid operands `X *' and `X *' to binary `operator +'
The compiler didn't accept your definition of operator +, so none is defined
at this point.

Btw: an operator+ that returns void is quite strange.

Jun 12 '07 #3

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

Similar topics

6
by: Shankar | last post by:
Hello, We have a smart pointer class which provides the dereference operator -> to access the underlying object pointer. Now, we have a new requirement where a different type of object (e.g from...
2
by: vivekian | last post by:
Have a pointer to an object of class type task task * A ; Now this object takes operator overloading like A<<2 ; which assigns the number 2 to one of the members of the object A.
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: mario.rossi | last post by:
Hello! I would like to write a pointer to type template that behaves exactly and completely like normal pointers, which I will use as starting point for other kinds of pointers that hold extra...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
5
by: Schizoid Man | last post by:
Hello, Suppose I have a class called ModDate that contains three private members - day, month, year - all defined as int. I want to overload the += operator and add an integer value to the...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
4
by: Encrypted | last post by:
i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. after the addition i want to return the local object...
26
by: eotcl | last post by:
Dear Group! Is it possible to dereference the void pointer to function pointer, and refer to this pointer later as if it was a function. Thanks.
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
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,...
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,...
1
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...
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...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.