Click here to Skip to main content
15,867,686 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRead Access exception in xtree for map::insert Pin
ForNow24-Dec-22 17:18
ForNow24-Dec-22 17:18 
AnswerRe: Read Access exception in xtree for map::insert answer found Pin
ForNow25-Dec-22 9:55
ForNow25-Dec-22 9:55 
Questionoverloaded = operator not being invoked Pin
ForNow22-Dec-22 14:36
ForNow22-Dec-22 14:36 
AnswerRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 15:24
Mircea Neacsu22-Dec-22 15:24 
GeneralRe: overloaded = operator not being invoked Pin
ForNow22-Dec-22 15:28
ForNow22-Dec-22 15:28 
GeneralRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 15:37
Mircea Neacsu22-Dec-22 15:37 
GeneralRe: overloaded = operator not being invoked Pin
ForNow22-Dec-22 16:00
ForNow22-Dec-22 16:00 
GeneralRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 16:32
Mircea Neacsu22-Dec-22 16:32 
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 Smile | :)

If you have something like:
C++
struct A {
  char ch;
  long l;
//...
};

struct B {
  int i;
  char c;
//...
};

A a;
B b;


If you write an assignment:
C++
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:
C++
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:
C++
struct A {
  char ch;
  long l;
//...
  int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
};

After that we can write:
C++
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:
C++
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:
C++
A a1;
a1 = a = b;
we will get an error because the result of
C++
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:
C++
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

GeneralRe: overloaded = operator not being invoked Pin
ForNow22-Dec-22 16:41
ForNow22-Dec-22 16:41 
QuestionStill Struggling with map::insert and operator overload Pin
ForNow20-Dec-22 15:57
ForNow20-Dec-22 15:57 
AnswerRe: Still Struggling with map::insert and operator overload Pin
Mircea Neacsu20-Dec-22 17:34
Mircea Neacsu20-Dec-22 17:34 
GeneralRe: Still Struggling with map::insert and operator overload Pin
ForNow21-Dec-22 1:51
ForNow21-Dec-22 1:51 
QuestionMessage Closed Pin
18-Dec-22 5:01
ForNow18-Dec-22 5:01 
AnswerRe: Question about return value of STD MAP::INSERT Pin
Mircea Neacsu18-Dec-22 6:55
Mircea Neacsu18-Dec-22 6:55 
GeneralRe: Question about return value of STD MAP::INSERT Pin
ForNow18-Dec-22 7:08
ForNow18-Dec-22 7:08 
GeneralRe: Question about return value of STD MAP::INSERT Pin
ForNow18-Dec-22 8:05
ForNow18-Dec-22 8:05 
AnswerRe: Question about return value of STD MAP::INSERT Pin
Mircea Neacsu18-Dec-22 8:14
Mircea Neacsu18-Dec-22 8:14 
GeneralRe: Question about return value of STD MAP::INSERT Pin
ForNow18-Dec-22 8:31
ForNow18-Dec-22 8:31 
Questionhow to typedef array of 4 char to be used in map template Pin
ForNow13-Dec-22 14:17
ForNow13-Dec-22 14:17 
AnswerRe: how to typedef array of 4 char to be used in map template Pin
CPallini13-Dec-22 21:05
mveCPallini13-Dec-22 21:05 
GeneralRe: how to typedef array of 4 char to be used in map template Pin
ForNow14-Dec-22 1:47
ForNow14-Dec-22 1:47 
GeneralRe: how to typedef array of 4 char to be used in map template Pin
CPallini14-Dec-22 2:17
mveCPallini14-Dec-22 2:17 
GeneralRe: how to typedef array of 4 char to be used in map template Pin
ForNow14-Dec-22 2:30
ForNow14-Dec-22 2:30 
GeneralRe: how to typedef array of 4 char to be used in map template still getting compile errors Pin
ForNow15-Dec-22 2:24
ForNow15-Dec-22 2:24 
GeneralRe: how to typedef array of 4 char to be used in map template still getting compile errors Pin
CPallini15-Dec-22 3:01
mveCPallini15-Dec-22 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.