Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Inherited operator= malfunctioning (http://www.programmingforums.org/showthread.php?t=14972)

Lakrids Jan 18th, 2008 12:22 PM

Inherited operator= malfunctioning
 
Hello,

I have a weird problem with an inherited operator=. Here's a simplified code of the situation :

:

  1. class A {
  2.   public:
  3.       A& operator=(const A&);
  4. };
  5.  
  6. class B : public A {
  7.   public:
  8.       B& operator=(const B&);
  9.       B& operator=(const A&);
  10. };


Now, this works FINE:

:

  1. B& B::operator=(const A& a) {
  2.     this->A::operator=(a);
  3.     return (*this);
  4. }


gives the accurate results. However, this doesn't :

:

  1. B& B::operator=(const B& b) {
  2.     this->A::operator=(b);
  3.     return (*this);
  4. }


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

peaceofpi Jan 18th, 2008 12:42 PM

Re: Inherited operator= malfunctioning
 
Quote:

Originally Posted by Lakrids (Post 139838)
:

  1. B& B::operator=(const B& b) {
  2.     this->A::operator=(b);
  3.     return (*this);
  4. }


I'm guessing (literally, not sarcastically) that the problem is the second line: class A doesn't have an overloaded operator= for a type B.

Lakrids Jan 18th, 2008 1:11 PM

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?

grumpy Jan 18th, 2008 4:18 PM

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.

Lakrids Jan 19th, 2008 5:32 AM

Re: Inherited operator= malfunctioning
 
Quote:

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.
Yeah, your right as usual Grumpy. Now it works. Thanks!


All times are GMT -5. The time now is 3:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC