CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

operator+ in derived classes More options topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 03-30-2007, 01:17 AM
Junior Member
 
Join Date: Mar 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
JimLangston is on a distinguished road
| More
operator+ in derived classes More options

This is something someone was asking in irc. I really don't need to do this
right now, but may have to in the future. The following code is in error
(marked).
Code:
#include <iostream> 
#include <string> 


class Base 
{ 
public: 
    Base( const int x = 0): x_( x ) {} 
    Base operator+( const Base b ) { Base Temp( x_ ); Temp.x_ += b.x_; 
return Temp; } 
    int X() { return x_; } 
    virtual ~Base() {} 
private: 
    int x_; 



}; 


class Derived: public Base 
{ 
public: 
    Derived( const int x = 0, const int y = 0): Base( x ), y_( y ) {} 
    int Y() { return y_; } 
private: 
    int y_; 


}; 


int main() 
{ 

    Base MyBase(10); 
    std::cout << MyBase.X() << "\n"; 


    Base MyBase2 = MyBase + Base(5); 
    std::cout << MyBase2.X() << "\n"; 


    Derived MyDerived( 10, 20 ); 
    std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n"; 


    Derived MyDerived2 = MyDerived + Derived( 5, 10 ); 
    // Following line gives compilation error 
    std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n"; 
   //  error C2440: 'initializing' : cannot convert from 'Base' to 'Derived' 
   //      No constructor could take the source type, or constructor 
overload resolution was ambiguous 


    std::string wait; 
    std::getline( std::cin, wait ); 



}
I understand the error. I am tryign to operator+ on derived, but the only operator + is on base.

How do people handle this?

What would be a nice solution, if possible, is to have a derived operator+ that calls the base operator+ for the addition of the x_'s, then add the y_'s in derived operator+ and return derived. I couldn't come up with a way to do this however, and think I would have to totally rewrite the operator+ for derived. In this case it's trivial, but I could see a complex class where it wouldn't be so trivial.

Any thoughts?
Reply With Quote
Reply

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
zero shift More options Mike C Programming 0 03-30-2007 01:15 AM
Operator New help More options John C++ Programming 0 03-30-2007 01:13 AM
problem with int_type.... More options SpreadTooThin C++ Programming 0 03-30-2007 01:09 AM
Overloading operator<< as a friend to a template class CrimzonRJ C++ Programming 1 12-26-2006 01:23 AM
changing variable types of a function of a derived class mrclash C# Programming 0 09-20-2006 04:56 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:59 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board