B
bberu
Ivan Vecerina and Swampmonster wrote:
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 ?
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 ?