Operatory wspornika przeciążeniowego [], aby uzyskać i ustawić
Mam następującą klasę:
class risc { // singleton
protected:
static unsigned long registers[8];
public:
unsigned long operator [](int i)
{
return registers[i];
}
};
Jak widzisz zaimplementowałem operator w nawiasach kwadratowych dla "getting".
Teraz chciałbym zaimplementować go do Ustawienia, np.: risc[1] = 2
.
43
2 answers
Spróbuj tego:
class risc { // singleton
protected:
static unsigned long registers[8];
public:
unsigned long operator [](int i) const {return registers[i];}
unsigned long & operator [](int i) {return registers[i];}
};
58
Author: Andrew Durward,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-16 19:57:14
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-16 19:57:14
Musisz zwrócić referencję z operator[]
, aby użytkownik klasy użył jej do ustawienia wartości. Więc sygnatura funkcji to unsigned long& operator [](int i)
.
10
Author: Naveen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-16 19:56:33
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-16 19:56:33