Code:
#ifndef ARREGLO_HPP
#define ARREGLO_HPP
using std::istream;
using std::ostream;
class Arreglo{
friend ostream& operator<<(ostream&, const Arreglo&);
friend istream& operator>>(istream&, Arreglo&);
public:
Arreglo(int = 10);
Arreglo(const Arreglo&);
~Arreglo();
int obtieneTamanio() const;
const Arreglo& operator=(const Arreglo&);
bool operator==(const Arreglo&) const;
bool operator!=(const Arreglo&) const;
int& operator[](int);
const int& operator[](int) const;
private:
int tamanio;
int *ptr;
};
#endif
How the hell is possible to access private variable using these constructors and functions?:
Code:
Arreglo::Arreglo(const Arreglo& arregloACopiar): tamanio(arregloACopiar.tamanio){
ptr = new int[tamanio];
for(int i = 0; i < tamanio; ++i)
ptr[i] = arregloACopiar.ptr[i];
}
The argument is an object of this same class, how is it possible to access private variable "tamanio" using "arregloACopiar.tamanio" and how the hell is possible to access private variable "ptr" using "arregloACopiar.ptr[i]"
Same here:
Code:
const Arreglo& Arreglo::operator=(const Arreglo& derecha){
if(&derecha != this){
if(tamanio != derecha.tamanio){
delete[] ptr;
tamanio = derecha.tamanio;
ptr = new int[tamanio];
}
for(int i = 0; i < tamanio; ++i)
ptr[i] = derecha.ptr[i];
}
return *this;
}
"derecha.tamanio" and "derecha.ptr[i]" are both ilegal because "tamanio" and "ptr" are private variables.
I have a full working code using those functions, no compiling errors, no runtime errors, everything goes fine. I just don't understand why it works...
explain me, please.