for whatever reason coding in the main(): className[index]= 2
. triggers the constructor of className instead of the coded overloading method where The right hand operand is passsed as parameter(2 in this case). Obscure behavior that needs your clarifications.
Why is the method operator[] is ignored ?
Here's a sample of dynamic arrayClass :
class dTableau
{
public:
dTableau(int taille);
int& operator [](int index) ;
dTableau &operator =(int val);
// other methods
private:
int *m_tab;
int m_taille;
};
dTableau::dTableau(int taille)
{
m_taille = taille;
m_tab = new int[taille];
for (int cnt = 0; cnt < m_taille;cnt++) {
m_tab[cnt] = cnt;
}
}
int& dTableau::operator[](const int index) {
return m_tab[index];
}
void main(){
int taille=2;
dTableau *t = new dTableau(taille);
// this line triggers the constructor with 1 as parameter
t[1]=1;
}