[英]How to use a member function for diferent objects of same class
我使用for創建了一個類的向量,並像這樣從.txt初始化成員變量
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();
}
我有一個CClass類的成員函數,該函數返回一個int:
int
CClass::Suma (int X, int Y){
total = X + Y;
return total;
}
然后,該向量具有10個“ CClass”類,每個類都有一個“ suma”功能。 所以我想通過添加每個“ suma”函數來獲得總結果,但是我不知道該怎么做。 我imgine這將是這樣的:
int resultado = 0;
for (i=0; i<10; i++){
resultado = resultado + m_Clase.Suma[i];
}
但它不起作用。
resultado += m_Clase [i].Suma ();
這是假設Suma使用成員函數,否則為什么還要在類中使用它呢? 如果使用非成員變量,則可以將其設為靜態函數,而不用擔心調用哪個實例:
resultado += CClass::Suma (X, Y);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.