![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2007
Posts: 41
Rep Power: 0
![]() |
Inherited operator= malfunctioning
Hello,
I have a weird problem with an inherited operator=. Here's a simplified code of the situation : cplusplus Syntax (Toggle Plain Text)
Now, this works FINE: cplusplus Syntax (Toggle Plain Text)
gives the accurate results. However, this doesn't : cplusplus Syntax (Toggle Plain Text)
Any idea why? It compiles fine, but I get very inaccurate results. I had several ideas, but none of them solved the problem : -putting 'this->' before every private variable of class A -converting b into an A using every _cast possible Any ideas? Thank you |
|
|
|
|
|
#2 | |
|
hi: for(;;) goto hi;
|
Re: Inherited operator= malfunctioning
Quote:
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Dec 2007
Posts: 41
Rep Power: 0
![]() |
Re: Inherited operator= malfunctioning
Yes, but if you pass an object B to it, it will automatically be converted into an object A. So no need for such an operator! Also, I am not allowed to add such a method into A (project restrictions).
What do you think would be a good solution? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,318
Rep Power: 5
![]() |
Re: Inherited operator= malfunctioning
Depends on your notion of "best".
The reason for the "inaccurate results" in the second B::operator=() is probably that you're slicing the objects. You need to do something with the B part of the left hand side. It is also possible you have no need to implement B's assignment operators at all. If you don't do it the compiler will supply defaults (which essentially do assignments of individual members of B after calling the assignment operator for the base class). If that is sufficient, then you don't need to implement B's assignment operator at all. From a design perspective, it is generally considered a bad idea for a base class to be non-abstract. If you follow that guideline, you will usually have no need for B &operator=(const A &); in class B anyway. |
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Dec 2007
Posts: 41
Rep Power: 0
![]() |
Re: Inherited operator= malfunctioning
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to add inherited control to toolbox. | InfoGeek | C# | 3 | Feb 8th, 2007 10:12 PM |
| Size of inherited class! | sharadpro | C++ | 3 | Feb 4th, 2007 2:43 PM |