简体   繁体   中英

How to use a member function for diferent objects of same class

I have created a vector of classes using a for, and initializing the member variables from a .txt like this

ifstream  fitxer(fichero);

if (fitxer.is_open())
{

int = 0;

delete[] m_Clase; //m_Clase is a pointer declared in main.h

m_Clase = new CClass [10];

for (i=0; i<10; i++){

  is >> m_Clase[i];
}

fitxer.close();
}

And i have a member function of the class CClass, that returns an int:

int
CClass::Suma (int X, int Y){

total = X + Y;

return total;
}

The vector then has 10 'CClass' classes, with a 'suma' fucntion each one. So I would like to get the total result from adding every 'suma' function, but i dont know how to do it. I imgine it would be something like this:

int resultado = 0;
for (i=0; i<10; i++){
resultado = resultado + m_Clase.Suma[i];
}

but it doesnt work.

resultado += m_Clase [i].Suma ();

This is assuming Suma uses member functions, otherwise why bother having it in the class? If it uses non member variables, you could just make it a static function an not worry about which instance is being called:

resultado += CClass::Suma (X, Y);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM