c++ - How to overload the new operator -
i tried overloading += get's me error :
expression must have integral or unscoped enum type
class sensor{}; class sensorlist{ sensor ** sensors; }; void main(){ sensor s1; sensorlist *list = new sensorlist(); list += &s1; }
this overloading of += operator
sensorlist* operator+=(sensor * sens){ sensorlist * lista= new sensorlist(); sensor ** temp = new sensor*[this->noelem]; (int = 0; < this->noelem; i++) temp[i] = this->senzori[i]; delete[] this->senzori; this->senzori = new sensor*[this->noelem + 1]; (int = 0; < this->noelem; i++) this->senzori[i] = temp[i]; this->senzori[this->noelem] = sens; this->noelem += 1; lista = this; return lista; }
how should it? have overload new operator?
however user defined operator+=
of yours defined, both operands of
list += &s1;
are pointers. never call user defined operator none of operands class
or enum
type. cannot overload operators 2 pointers because of language rules.
instead tries add pointer, , works integrals , enums, compiler tells you.
in particular, problem has nothing operator new
, , apparently did not try overload that.
so instead: stop doing through pointers , overload operators class or references class instead. this:
sensor s1; sensorlist sl; sl += s1;
i recommend read this.
if really feel need use pointers (you shouldn't), use free function like
void add(sensorlist*, sensor*);
Comments
Post a Comment