473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

operator+ in derived classes

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).

#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?
Mar 30 '07 #1
7 1701

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7h*******************@newsfe02.lga...
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).

#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";
My bad, the following line is the one in error, not the one below it. It's
the operator+ that's causing the problem, not the X() and Y();

Derived MyDerived2 = MyDerived + Derived( 5, 10 );
// error C2440: 'initializing' : cannot convert from 'Base' to 'Derived'
// No constructor could take the source type, or constructor
std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n";
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?

Mar 30 '07 #2
Jim Langston wrote:
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7h*******************@newsfe02.lga...
>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).

#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 ) {}
Add here

Derived(const Base& b) : Base(b), y_(0) {}
> 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";

My bad, the following line is the one in error, not the one below it.
It's the operator+ that's causing the problem, not the X() and Y();

Derived MyDerived2 = MyDerived + Derived( 5, 10 );
// error C2440: 'initializing' : cannot convert from 'Base' to
'Derived' // No constructor could take the source type, or
constructor
> std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n";
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?
Add the constructor from Base.
>>[..]

Any thoughts?
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 30 '07 #3
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:Pf******************************@comcast.com. ..
Jim Langston wrote:
>"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7h*******************@newsfe02.lga...
>>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).

#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 ) {}

Add here

Derived(const Base& b) : Base(b), y_(0) {}
>> 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";

My bad, the following line is the one in error, not the one below it.
It's the operator+ that's causing the problem, not the X() and Y();

Derived MyDerived2 = MyDerived + Derived( 5, 10 );
// error C2440: 'initializing' : cannot convert from 'Base' to
'Derived' // No constructor could take the source type, or
constructor
>> std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n";
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?

Add the constructor from Base.
That semi works. I.E. After fixing typos (actually displaying MyDerived2
values) my output is:
10
15
10 20
15 0

When I would want output of:
10
15
10 20
15 30

So I'm thinking I'm going to need to add y_ to y_ in derived. I tried to
add an operator+ to Derived with no success in getting what I want.

Derived operator+( const Derived& d ) { Derived Temp( X(), y_ ); Temp.y_ +=
d.y_; return Temp; }

Output is now
10
15
10 20
10 30

So it seems I can either add the base variables, or the derived variables,
but not both. I tried this but can't get it to compile

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 ) {}
Derived operator+( const Derived& d )
{
// Error on following line
Base TempBase = Base( this->X() ) + Base( d.X() );
// error C2662: 'Base::X' : cannot convert 'this' pointer from
'const Derived' to 'Base &'
Derived Temp( TempBase.X(), y_ );
Temp.y_ += d.y_;
return Temp;
}
int Y() { return y_; }
private:
int y_;
};

I can't figure out why I'm getting that error. It seems I can't get d.X().
I tried int TempInt = d.X() with the same error. I know outside the class
in mainline I can get a derived.X(); why cant' I in the operator+?
Mar 30 '07 #4
On 3ÔÂ30ÈÕ, ÉÏÎç10ʱ06·Ö, "Jim Langston" <tazmas...@rocketmail.comwrote:
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).

#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?
Derived& operator+(const Derived &d){
this->Base::operator+(d);
this->y_ += d.y_;
return *this;
}

Mar 30 '07 #5

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:If*****************@newsfe06.lga...
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:Pf******************************@comcast.com. ..
>Jim Langston wrote:
>>"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7h*******************@newsfe02.lga...
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).

#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 ) {}

Add here

Derived(const Base& b) : Base(b), y_(0) {}
>>> 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";

My bad, the following line is the one in error, not the one below it.
It's the operator+ that's causing the problem, not the X() and Y();

Derived MyDerived2 = MyDerived + Derived( 5, 10 );
// error C2440: 'initializing' : cannot convert from 'Base' to
'Derived' // No constructor could take the source type, or
constructor
std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n";
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?

Add the constructor from Base.

That semi works. I.E. After fixing typos (actually displaying MyDerived2
values) my output is:
10
15
10 20
15 0

When I would want output of:
10
15
10 20
15 30
<Snipped trial and error til I got it right>

This is what I wound up donig. Is this the way you would necessarily do it?

#include <iostream>
#include <string>

class Base
{
public:
Base( const int x = 0): x_( x ) {}
Base operator+( const Base b ) const { Base Temp( x_ ); Temp.x_ += b.x_;
return Temp; }
int X() const { 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 ) {}
Derived operator+( const Derived& d ) const
{
Base TempBase = Base( this->X() ) + Base( d.X() );
Derived Temp( TempBase.X(), y_ );
Temp.y_ += d.y_;
return Temp;
}
int Y() const { 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 );
std::cout << MyDerived2.X() << " " << MyDerived2.Y() << "\n";

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

}

Output is my wanted:
10
15
10 20
15 30
Mar 30 '07 #6
"c++ seeker" <te*****@gmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
On 3ÔÂ30ÈÕ, ÉÏÎç10ʱ06·Ö, "Jim Langston" <tazmas...@rocketmail.comwrote:
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).

#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?
Derived& operator+(const Derived &d){
this->Base::operator+(d);
this->y_ += d.y_;
return *this;
}

Output of that is:
10
15
10 20
10 30

Instead of
10
15
10 20
15 30

Also, it changes this, but += is not what I wanted.
Mar 30 '07 #7
Jim Langston wrote:
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:If*****************@newsfe06.lga...
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:Pf******************************@comcast.com ...
>>Jim Langston wrote:
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7h*******************@newsfe02.lga...
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).
>
#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 )
{}

Add here

Derived(const Base& b) : Base(b), y_(0) {}

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";

My bad, the following line is the one in error, not the one below
it. It's the operator+ that's causing the problem, not the X() and
Y(); Derived MyDerived2 = MyDerived + Derived( 5, 10 );
// error C2440: 'initializing' : cannot convert from 'Base' to
'Derived' // No constructor could take the source type, or
constructor
std::cout << MyDerived.X() << " " << MyDerived.Y() << "\n";
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?

Add the constructor from Base.

That semi works. I.E. After fixing typos (actually displaying
MyDerived2 values) my output is:
10
15
10 20
15 0

When I would want output of:
10
15
10 20
15 30
<Snipped trial and error til I got it right>

This is what I wound up donig. Is this the way you would necessarily
do it?
#include <iostream>
#include <string>

class Base
{
public:
Base( const int x = 0): x_( x ) {}
Base operator+( const Base b ) const { Base Temp( x_ ); Temp.x_ +=
b.x_; return Temp; }
It should really be shorter:

Base operator+(const Base& b) const { return Base(b.x_ + x_); }
int X() const { 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 ) {}
Derived operator+( const Derived& d ) const
{
Base TempBase = Base( this->X() ) + Base( d.X() );
Derived Temp( TempBase.X(), y_ );
Temp.y_ += d.y_;
return Temp;
}
Yep, something like that. You don't really need to construct new Base
objects from X(). You could just cast '*this' and 'd' to Base&:

Derived operator+( const Derived& d ) const
{
Base TempBase(Base(*this) + Base(d)); // fall back on Base::op+
return Derived( TempBase.X(), y_ + d_.y );

}
int Y() const { 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 );
std::cout << MyDerived2.X() << " " << MyDerived2.Y() << "\n";

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

}

Output is my wanted:
10
15
10 20
15 30
See above. I don't know the output -- no time to try, and too lazy
to just look and figure it out. However, it might not be what you
want. Generally, you do want to fall back on Base::operator+ as
much as possible instead of adding 'x_' values yourself. For that
you might want to make operator+ virtual...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 30 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
5
by: noone | last post by:
hi. I don't use exceptions much in the embedded world, but for my plugin interface to a hardware MPEG encoder I'd like to, since there are so many places that the crummy kernel driver can do bad...
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
17
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.