- sh4d0w: Netflix? Ugyan, VW előfizetés!
- Elektromos rásegítésű kerékpárok
- WhrlpoolMind: Az eredeti lemez utánozhatatlan illata
- sziku69: Fűzzük össze a szavakat :)
- Geri Bátyó: Megint tahó voltam – SZEMÉLYISÉGFEJLŐDÉS
- Luck Dragon: Asszociációs játék. :)
- Meggyi001: Nyilvános wc-k.....még mindig hiánypótló...
- sziku69: Szólánc.
- Mr Dini: Mindent a StreamSharkról!
- GoodSpeed: AMD Ryzen 7 7700X vs AMD Ryzen 9 9900X Cinebench R23 & R24 Benchmarkokban mérve
Új hozzászólás Aktív témák
-
Jester01
veterán
Ha megoldható, akkor a tömböt add át, és a függvényen belül legyen a .adatx. Ez gondolom menne, tehát valamiért nem megvalósítható.
Alternatívaként csinálhatsz accessor függvényt, varázsolhatsz offsetof segítségével avagy ha esetleg véletlen c++ és nem c akkor member pointerrel.
#include <iostream>
using namespace std;
struct Foo
{
int x;
int y;
};
int sum0_x(Foo* array, int count)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].x;
}
return r;
}
int sum0_y(Foo* array, int count)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].y;
}
return r;
}
int sum1(Foo* array, int count, int (*accessor)(Foo*))
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += accessor(array + i);
}
return r;
}
int sum2(Foo* array, int count, int member)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += *(int*)((char*)(array + i) + member);
}
return r;
}
int sum3(Foo* array, int count, int (Foo::*member))
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].*member;
}
return r;
}
int accessor_x(Foo* foo)
{
return foo->x;
}
int accessor_y(Foo* foo)
{
return foo->y;
}
int main()
{
Foo foos[2];
foos[0].x = 1;
foos[0].y = 2;
foos[1].x = 3;
foos[1].y = 4;
cout << "sum0(x) = " << sum0_x(foos, 2) << endl;
cout << "sum0(y) = " << sum0_y(foos, 2) << endl;
cout << "sum1(x) = " << sum1(foos, 2, accessor_x) << endl;
cout << "sum1(y) = " << sum1(foos, 2, accessor_y) << endl;
cout << "sum2(x) = " << sum2(foos, 2, offsetof(Foo, x)) << endl;
cout << "sum2(y) = " << sum2(foos, 2, offsetof(Foo, y)) << endl;
cout << "sum3(x) = " << sum3(foos, 2, &Foo::x) << endl;
cout << "sum3(y) = " << sum3(foos, 2, &Foo::y) << endl;
return 0;
}
Új hozzászólás Aktív témák
Hirdetés
● olvasd el a téma összefoglalót!
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Wimius W2 okosprojektor / Számla + Garancia / 30 000 Ft
- Samsung Galaxy S23 128GB, Kártyafüggetlen, 1 Év Garanciával
- Bomba ár! Lenovo ThinkPad E14 G1: i5-10G I 16GB I 512SSD I HDMI I 14" FHD I Cam I W11 I Gari!
- MacBook Pro 16 i7-9750H 16GB RAM 512GB SSD RX 5300M 1 év garancia
- 24 hónapos PlayStation Plus Premium előfizetés a legolcsóbban, egyenesen a PlayStation-től!
Állásajánlatok
Cég: FOTC
Város: Budapest