Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 18th, 2008, 12:22 PM   #1
Lakrids
Programmer
 
Join Date: Dec 2007
Posts: 41
Rep Power: 0 Lakrids is on a distinguished road
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)
  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:

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

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

cplusplus Syntax (Toggle Plain Text)
  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
Lakrids is offline   Reply With Quote
Old Jan 18th, 2008, 12:42 PM   #2
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 123
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Inherited operator= malfunctioning

Quote:
Originally Posted by Lakrids View Post
cplusplus Syntax (Toggle Plain Text)
  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.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jan 18th, 2008, 1:11 PM   #3
Lakrids
Programmer
 
Join Date: Dec 2007
Posts: 41
Rep Power: 0 Lakrids is on a distinguished road
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?
Lakrids is offline   Reply With Quote
Old Jan 18th, 2008, 4:18 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,318
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Jan 19th, 2008, 5:32 AM   #5
Lakrids
Programmer
 
Join Date: Dec 2007
Posts: 41
Rep Power: 0 Lakrids is on a distinguished road
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!
Lakrids is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:08 PM.

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