overloaded = operator not being invoked
-
Hi my overloaded operator are in my header so I make a breakpoint watching them being invoked except for the = operator hope some can tell me why i'll post first type of struct using range
;
struct usingrange
{
ESDID esdid;
int start;
int end;
uint32_t operator=(const BYTE z[4])
{
return ((z[0] << 24) | (z[1] << 16) | (z[2] << 8) | z[3]);};
bool operator<(const usingrange x) const {
if (esdid < x.esdid) return esdid < x.esdid;
if (start < x.start) return start < x.start; if (end < x.end) return end < x.end;
};};
next my code with the = operator the lvalue is member of using range I would think that would casue invocation of my overloaded operator
usingrange using\_range{ usingpoint->usingesdid }; using\_range.start = (BYTE &)usingpoint->statement; using\_range.end = (BYTE &)usingpoint->laststatement;
-
Hi my overloaded operator are in my header so I make a breakpoint watching them being invoked except for the = operator hope some can tell me why i'll post first type of struct using range
;
struct usingrange
{
ESDID esdid;
int start;
int end;
uint32_t operator=(const BYTE z[4])
{
return ((z[0] << 24) | (z[1] << 16) | (z[2] << 8) | z[3]);};
bool operator<(const usingrange x) const {
if (esdid < x.esdid) return esdid < x.esdid;
if (start < x.start) return start < x.start; if (end < x.end) return end < x.end;
};};
next my code with the = operator the lvalue is member of using range I would think that would casue invocation of my overloaded operator
usingrange using\_range{ usingpoint->usingesdid }; using\_range.start = (BYTE &)usingpoint->statement; using\_range.end = (BYTE &)usingpoint->laststatement;
Again, not sure which one of the 3 statements you would expect to invoke the assignment operator. The first one:
usingrange using_range{ usingpoint->usingesdid };
is a declaration that invokes a constructor. As you don't have a constructor, the compiler creates a default one. The second and the third:
using_range.start = (BYTE &)usingpoint->statement;
using_range.end = (BYTE &)usingpoint->laststatement;are simply integer assignments. If you would want to invoke the assignment operator you would need to write something like
usingrange using_range2;
using_range2 = using_range;Mircea
-
Again, not sure which one of the 3 statements you would expect to invoke the assignment operator. The first one:
usingrange using_range{ usingpoint->usingesdid };
is a declaration that invokes a constructor. As you don't have a constructor, the compiler creates a default one. The second and the third:
using_range.start = (BYTE &)usingpoint->statement;
using_range.end = (BYTE &)usingpoint->laststatement;are simply integer assignments. If you would want to invoke the assignment operator you would need to write something like
usingrange using_range2;
using_range2 = using_range;Mircea
-
these two the lvalue is a int and a member of using range the rvalue is of type BYTE [4] ? I am sure I am not understanding something ?
using_range.start = *usingpoint->statement;
using_range.end = *usingpoint->laststatement;If
statement
isBYTE[4]
,usingpoint->statement
is a pointer to a BYTE value. So*usingpoint->laststatement
is the content of the first byte. It can be assigned to an integer without any problem. That's probably not what you want but that's what the compiler understood. The assignment operator that you defined is for assigning the WHOLE structure. It doesn't apply to individual members.Mircea
-
If
statement
isBYTE[4]
,usingpoint->statement
is a pointer to a BYTE value. So*usingpoint->laststatement
is the content of the first byte. It can be assigned to an integer without any problem. That's probably not what you want but that's what the compiler understood. The assignment operator that you defined is for assigning the WHOLE structure. It doesn't apply to individual members.Mircea
you are right I saw that in that disassembly however I made a breakpoint at "int operator+..." and it never got invoked probably because as you said the rvalue is BYTE while in the operator= parameter it BYTE z[4]; when I have the code
using_range.start = usingpoint->statement;
I got an error message from intellsense saying cannt assign int to BYTE *
-
you are right I saw that in that disassembly however I made a breakpoint at "int operator+..." and it never got invoked probably because as you said the rvalue is BYTE while in the operator= parameter it BYTE z[4]; when I have the code
using_range.start = usingpoint->statement;
I got an error message from intellsense saying cannt assign int to BYTE *
I think you misunderstood the functioning of overloaded operators. I'll try to explain but it's best if you check with a proper manual. I'm not a particularly good teacher :) If you have something like:
struct A {
char ch;
long l;
//...
};struct B {
int i;
char c;
//...
};A a;
B b;If you write an assignment:
a = b;
The compiler will complain that it doesn't know how to assign a B to an A. Kind of an apples and oranges problem. However you can write:
a.l = b.i;
a.ch = b.c;because those are standard types and compiler knows how to perform those assignments without needing a user defined assignment operator. Now we add a user defined assignment operator:
struct A {
char ch;
long l;
//...
int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
};After that we can write:
a = b;
and the compiler will know how to do the assignment. You can have many such assignment operators. For instance you might add another one to assign an A to another A:
struct A {
char ch;
long l;
//...
int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
int operator =(A& rhs) {l = rhs.l; ch = rhs.ch; return 1}
};The assignment operator that takes an object of the same kind on the right hand side is called "principal assignment operator". Note however that we cannot "chain" the assignment operator. If we write:
A a1;
a1 = a = b;we will get an error because the result of
a = b
is an
int
and cannot be assigned to an A. To respect the conventions we would have to change the signature of the principal assignment operator:struct A {
char ch;
long l;
//...
int operator =(const B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
A& operator =(const A& rhs) {l = rhs.l; ch = rhs.ch; return *this;}
};Note that I also made the right hand side a reference to a const. I hope you will find useful my little lecture on assignment operators but, again, best would be to check with a real manual.
Mircea
-
I think you misunderstood the functioning of overloaded operators. I'll try to explain but it's best if you check with a proper manual. I'm not a particularly good teacher :) If you have something like:
struct A {
char ch;
long l;
//...
};struct B {
int i;
char c;
//...
};A a;
B b;If you write an assignment:
a = b;
The compiler will complain that it doesn't know how to assign a B to an A. Kind of an apples and oranges problem. However you can write:
a.l = b.i;
a.ch = b.c;because those are standard types and compiler knows how to perform those assignments without needing a user defined assignment operator. Now we add a user defined assignment operator:
struct A {
char ch;
long l;
//...
int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
};After that we can write:
a = b;
and the compiler will know how to do the assignment. You can have many such assignment operators. For instance you might add another one to assign an A to another A:
struct A {
char ch;
long l;
//...
int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
int operator =(A& rhs) {l = rhs.l; ch = rhs.ch; return 1}
};The assignment operator that takes an object of the same kind on the right hand side is called "principal assignment operator". Note however that we cannot "chain" the assignment operator. If we write:
A a1;
a1 = a = b;we will get an error because the result of
a = b
is an
int
and cannot be assigned to an A. To respect the conventions we would have to change the signature of the principal assignment operator:struct A {
char ch;
long l;
//...
int operator =(const B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
A& operator =(const A& rhs) {l = rhs.l; ch = rhs.ch; return *this;}
};Note that I also made the right hand side a reference to a const. I hope you will find useful my little lecture on assignment operators but, again, best would be to check with a real manual.
Mircea