Ver Mensaje Individual
  #6 (permalink)  
Antiguo 25/02/2013, 12:39
fightmx
 
Fecha de Ingreso: febrero-2003
Ubicación: D.F.
Mensajes: 163
Antigüedad: 21 años, 2 meses
Puntos: 22
Respuesta: duda hacer referencia en objetos

A ver si el siguiente ejemplo aclara un poco, son dos vectores (int e int*), con el primero no puedo modificar los valores originales:

Código C++:
Ver original
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int main(){
  7.     int a = 1;
  8.     int b = 2;
  9.     int c = 3;
  10.  
  11.     vector<int> v1;
  12.     v1.push_back(a);
  13.     v1.push_back(b);
  14.     v1.push_back(c);
  15.  
  16.     v1[0] = 10;
  17.     v1[2] = 20;
  18.     v1[3] = 30;
  19.  
  20.     cout << a << " " << b << " " << c << endl;
  21.  
  22.     vector<int*> v2;
  23.     v2.push_back(&a);
  24.     v2.push_back(&b);
  25.     v2.push_back(&c);
  26.  
  27.     *v2[0] = 10;
  28.     *v2[1] = 20;
  29.     *v2[2] = 30;
  30.  
  31.     cout << a << " " << b << " " << c << endl;
  32.  
  33.     return 0;
  34. }