Redefining operators in C++

B

bberu

Ivan Vecerina and Swampmonster wrote:

bberu said:
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
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).

Hi,

I built a structure like this :

struct tMonVecteur
{
const tEntier Taille = 2;

tEntier Elements[Taille] = {10,20}; // Not ANSI but for the
Exemple

tMonVecteur* operator=(tMonVecteur Right)
{
tEntier compteur;

for (compteur=0; compteur<Taille; compteur++)
{
this->Elements[compteur] = Right.Elements[compteur];
}
return this;
}

tEntier operator[](tEntier Element)
{
return this->Elements[Element];
}

tMonVecteur operator+(tMonVecteur Right)
{
tMonVecteur temp;
tEntier compteur;

for (compteur=0; compteur<Taille; compteur++)
{
temp.Elements[compteur] = this->Elements[compteur] +
Right.Elements[compteur];
}
return temp;
}

tMonVecteur operator*(tMonVecteur Right)
{
tMonVecteur temp;
tEntier compteur;

for (compteur=0; compteur<Taille; compteur++)
{
temp.Elements[compteur] = this->Elements[compteur] *
Right.Elements[compteur];
}
return temp;
}

};


With this structure, the following operations work :

a = Vecteur1[1];
Vecteur1 = Vecteur1 + Vecteur2;
Vecteur3 = Vecteur1 * Vecteur2;

But I did not find the solution for that :

Vecteur1[1] = 10; => error when compiling
Vecteur1 = {1,2}; => error when compiling


Is there a way to do those two operations on the new tMonVecteur
structure ?
 
I

Ivan Vecerina

bberu said:
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).

Hi,

I built a structure like this :

struct tMonVecteur
{
const tEntier Taille = 2;

tEntier Elements[Taille] = {10,20}; // Not ANSI but for the
Exemple

tMonVecteur* operator=(tMonVecteur Right)
{
tEntier compteur;

for (compteur=0; compteur<Taille; compteur++)
{
this->Elements[compteur] = Right.Elements[compteur];
}
return this;
}

Style-wise, I think even French coders will usually prefer
to write, in C++:
for( tEntier i = 0 ; i<Taille ; ++i )
this->Elements = Right.Elements;
(more concise)

tEntier operator[](tEntier Element)
{
return this->Elements[Element];
}
For this to work as expected, you should use a reference
for the return type:
tEntier& operator[](tEntier Element)
{
return this->Elements[Element];
}
To be complete, you should also add the following
const overload to be used with const objects:
tEntier operator[](tEntier Element) const
{
return this->Elements[Element];
}
tMonVecteur operator+(tMonVecteur Right)
A better function signature would be:
tMonVecteur operator+(tMonVecteur const& Right)
This avoids an unnecessary copy of the parameter.
{
tMonVecteur temp;
tEntier compteur;
NB: putting all variables definitions at the beginning
of the function is an old C-ism. Since C99, you can
(and probably should, style-wise) declare variables
on first use.
for (compteur=0; compteur<Taille; compteur++)
{
temp.Elements[compteur] = this->Elements[compteur] +
Right.Elements[compteur];
}
return temp;
}

tMonVecteur operator*(tMonVecteur Right)
Same as above:
tMonVecteur operator*(tMonVecteur const& Right)
{
tMonVecteur temp;
tEntier compteur;

for (compteur=0; compteur<Taille; compteur++)
{
temp.Elements[compteur] = this->Elements[compteur] *
Right.Elements[compteur];
}
return temp;
}

};


With this structure, the following operations work :

a = Vecteur1[1];
Vecteur1 = Vecteur1 + Vecteur2;
Vecteur3 = Vecteur1 * Vecteur2;

But I did not find the solution for that :

Vecteur1[1] = 10; => error when compiling

This will work with the op[] overloaded
as suggested above.
Vecteur1 = {1,2}; => error when compiling


Is there a way to do those two operations on the new tMonVecteur
structure ?
If the structure does not define a constructor,
you can initialize a newly defined variable as follows:
tMonVecteur v = {1,2};
[ if this does not compile, it would be because of
the non-standard extension you are using ]

The alternative is to add a constructor to your vector
struct:
tMonVecteur() { Elements[0]=0; Elements[2]=0; }
tMonVecteur(tEntier x, tEntier y)
{ Elements[0]=x; Elements[2]=y; }
You can then write:
tMonVecteur v(5,7);
v = v + tMonVecteur(6,4); // v will then be 11,11



Salutations,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top