473,491 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Redefining operators in C++

Hi,

I know it is possible to refine operators (like +, -, * or /) on classes.
But is it possible on simple types ?
ex :
If I have a type like :
typedef int vector[100]
is it possible to redefine a + operator on the vetor type ?

Thanks !
Bruno
Jul 22 '05 #1
7 4534
bberu wrote:

Hi,

I know it is possible to refine operators (like +, -, * or /) on classes.
But is it possible on simple types ?
ex :
If I have a type like :
typedef int vector[100]
is it possible to redefine a + operator on the vetor type ?


vector is not a type.
vector is of type: array[100] of int

And no. It is not possible to define operators for the built in types.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
bberu wrote:
I know it is possible to refine operators (like +, -, * or /) on classes.
But is it possible on simple types ?
ex :
If I have a type like :
typedef int vector[100]
is it possible to redefine a + operator on the vetor type ?
vector is not a type.
vector is of type: array[100] of int


vector is indeed the name of a type (although a poorly chosen one).
And no. It is not possible to define operators for the built in types.


But the question was not about built-in types.

You can actually overload operators on some simple types such as enum:

enum SimpleType { stA, stB, stC };

SimpleType& operator++(SimpleType& a) {
if(a>=stC) a = stA; else a = static_cast<SimpleType>(a+1);
return a;
}
However, while functions can be overloaded on array parameters,
overloaded operators need to have at least one parameter of
a user-defined type:

typedef int vec3[3];

void add( vec3& a, vec3 const& b ) // ok, fully valid
{
a[0]+=b[0]; a[1]+=b[1]; a[2]+=b[2];
}

void operator++( vec3& a ) // ERROR - op overload requires UDT
{
a[0]+=1; a[1]+=1; a[2]+=1;
}
Anyway, array types have many caveats and limitations
(e.g. implicit conversion to pointers;
cannot be used as function return value).

It is much safer and easier to use a struct wrapping
the fixed-size array:
struct vec100
{
int m[100];
};
All operators can then be overloaded . By adding operator[]
members, you can also support the same syntax as for a
built-in array (and have the possibility to add bounds checks).

Boost and most likely the next version of the standard library
also include a template class that automates this process:
typedef array<int,100> vec100;
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #3
Ivan Vecerina wrote:
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
But the question was not about built-in types.

You can actually overload operators on some simple types such as enum:

enum SimpleType { stA, stB, stC };
To overload an operator, one operand must be of class or enum type.
Enum is NOT a simple type.

However, while functions can be overloaded on array parameters,
Nope. Any array in a function argument is converted to pointer to
the element type for the purposes of overloading.
void add( vec3& a, vec3 const& b ) // ok, fully valid
and identical to:
void add(int*&, int*const& )
for overloading purposes. This is an important distinction as:
typedef int vec4[4];
void add(vec4, vec4);
void add(vec3, vec3);
yield the same signature and make the program ill-formed.

It is much safer and easier to use a struct wrapping


Jul 22 '05 #4
I know it is possible to refine operators (like +, -, * or /) on classes.
But is it possible on simple types ?
ex :
If I have a type like :
typedef int vector[100]
is it possible to redefine a + operator on the vetor type ?


Hi!

vector is NOT a type. vector is a type-alias for "int [100]"
So no, you can't overload operators, since at least one argument of on
overloaded operator needs to be of class-type (user defined type).
You might do something like:
struct my_vector
// don't just use "vector" because everybode who reads it will
// immediately think so std::vector<> which is a completely different
// thing
{
int elements[100];
};

Then you can overload any operator + or whatever you like.
If you want you can overload operator [] to enable direct access to the
elements. You might even check the index inside that operator, which is
a nice thing to have, at least for debug builds.
Bye,
'monster
Jul 22 '05 #5
"Ron Natalie" <ro*@sensor.com> wrote in message
news:41***********************@news.newshosting.co m...
Ivan Vecerina wrote:
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message But the
question was not about built-in types.

You can actually overload operators on some simple types such as enum:

enum SimpleType { stA, stB, stC };
To overload an operator, one operand must be of class or enum type.
Enum is NOT a simple type.


Does your claim have any basis in the C++ standard?
From a naive perspective, an enum is pretty much a simple type.
From a formal perspective, SimpleType would qualify as
a "Simple type specifier"(§7.1.5).
But the real point is that I don't think that the unspecific
wording should be interepreted to the detriment of the OP.
However, while functions can be overloaded on array parameters,


Nope. Any array in a function argument is converted to pointer to
the element type for the purposes of overloading.


Not if this array is passed by reference, as in the example
I provided.
See §8.3.5: the decay of array to pointer applies if the
parameter is of type "array of T". This does not include
parameters that would be of type "reference to array of T".
void add( vec3& a, vec3 const& b ) // ok, fully valid


and identical to:
void add(int*&, int*const& )
for overloading purposes.


Not true - a reference to a pointer is not equivalent to
a reference to an array.
Did you try to compile the sample code I had provided ?

This is an important distinction as:
typedef int vec4[4];
void add(vec4, vec4);
void add(vec3, vec3);
yield the same signature and make the program ill-formed.


Yes - in your modified example.
But the following functions have distinct signatures:
void add(vec4&, vec4&);
void add(vec3&, vec3&);

It is much safer and easier to use a struct wrapping

And we naturally agree on that, I believe.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #6
Ivan Vecerina wrote:
To overload an operator, one operand must be of class or enum type.
Enum is NOT a simple type.

Does your claim have any basis in the C++ standard?
From a naive perspective, an enum is pretty much a simple type.


Certainly, or I would not have made it. 3.9.2 defines enum
as a compound type.
From a formal perspective, SimpleType would qualify as
a "Simple type specifier"(§7.1.5).
A simple type specifier is just one that can be expressed as
a single word. It can be anything from a class to a int.
There are fundamental types (the proper antonym to compound
type) that are not expressed with simple type specifiers:
unsigned short
is not.
std::exception
is.

Jul 22 '05 #7
"Ron Natalie" <ro*@sensor.com> wrote in message
news:41***********************@news.newshosting.co m...
Ivan Vecerina wrote:
To overload an operator, one operand must be of class or enum type.
Enum is NOT a simple type.

Does your claim have any basis in the C++ standard?
From a naive perspective, an enum is pretty much a simple type.


Certainly, or I would not have made it. 3.9.2 defines enum
as a compound type.


So let's call it a simple compound type ;)
I just disagree that "simple" had to imply "fundamental"
in the context of the OP.

Kind regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #8

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

Similar topics

14
2481
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
1
2356
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms....
1
860
by: bberu | last post by:
> Ivan Vecerina and Swampmonster wrote: > > bberu wrote: > >> I know it is possible to refine operators (like +, -, * or /) on classes. > >> But is it possible on simple types ? > >> ex : >...
3
1923
by: notme | last post by:
I have code with lots of "new int..." etc.. I'd like to replace every instance with "new(allocInfo) int" (that is, using placement new) I tried doing: #define new (new(allocInfo)) but that...
2
3445
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
49
14854
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
17
2418
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
4
3876
by: allan.mcrae | last post by:
As part of a very simple memory leak detector, I am trying to store the value of __FILE__ in a char*. Since gcc4.2 I get the following warning... warning: deprecated conversion from string...
4
1856
by: Belebele | last post by:
The following code fails to compile. My intention is to provide different definitions for a nested class for a class template partial specialization. Here it is: template <typename , int class...
0
7115
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
6978
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
7154
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
6858
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
7360
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
5451
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,...
1
4881
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
1392
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
280
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...

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.