 |
| C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

January 1st, 2006, 12:29 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Unary operator overload and inheritance
Hi guys, and a happy new year...
I'm having some trouble inheriting overloaded operators - I've put together this little piece of code to illustrate my grievance...
It all works fine when I overload the prefix-operator - but if I overload the postfix-operator in a derived class, I get into all kinds of trouble - please give me some pointers!
Code:
/*
** Overloading and inheritance
** tested using overloads of unary operators
** 051231 Jonax
*/
#include <iostream.h>
#include <conio.h>
class cApple
{
protected:
int iWormCount;
float fTotalLengthOfWorms;
public:
cApple()
{
iWormCount = 0;
fTotalLengthOfWorms = 0;
}
cApple(int iWC, float fTLoW)
{
iWormCount = iWC;
fTotalLengthOfWorms = fTLoW;
}
cApple operator ++ ()
{
return cApple(++iWormCount, ++fTotalLengthOfWorms);
}
void put()
{
cout << "Number of worms: " << iWormCount << endl;
cout << "Total length of worms: " << fTotalLengthOfWorms << endl;
}
};
class cBigApple:public cApple
{
private:
float fWeightInKilos;
public:
cBigApple() : cApple()
{
fWeightInKilos = 0;
}
cBigApple(int iWC, float fTLoW, float fWiK) : cApple(iWC, fTLoW)
{
fWeightInKilos = fWiK;
}
cBigApple operator -- ()
{
return cBigApple(--iWormCount, --fTotalLengthOfWorms, 0.0);
}
};
class cBigRedApple:public cBigApple
{
private:
public:
cBigRedApple() : cBigApple()
{
}
cBigRedApple(int iWC, float fTLoW, float fWiK) : cBigApple(iWC, fTLoW, fWiK)
{
}
cBigRedApple operator -- (int)
{
return cBigRedApple(iWormCount--, fTotalLengthOfWorms--, 0.0);
}
};
int main(){
cApple oMyApple;
++oMyApple;
cout << "oMyApple:" << endl;
oMyApple.put();
cBigApple oMyBigApple;
++oMyBigApple;
++oMyBigApple;
++oMyBigApple;
cout << "\noMyBigApple:" << endl;
oMyBigApple.put();
cBigRedApple oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
--oMyBigRedApple;
oMyBigRedApple--;
cout << "\noMyBigRedApple:" << endl;
oMyBigRedApple.put();
getch();
return 0;
}
|

January 1st, 2006, 02:34 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
See the change
#include <iostream>
using namespace std;
class cApple
{
protected:
int iWormCount;
float fTotalLengthOfWorms;
public:
cApple()
{
iWormCount = 0;
fTotalLengthOfWorms = 0;
}
cApple(int iWC, float fTLoW)
{
iWormCount = iWC;
fTotalLengthOfWorms = fTLoW;
}
cApple operator ++ ()
{
return cApple(++iWormCount, ++fTotalLengthOfWorms);
}
void put()
{
cout << "Number of worms: " << iWormCount << endl;
cout << "Total length of worms: " << fTotalLengthOfWorms << endl;
}
};
class cBigApple:public cApple
{
private:
float fWeightInKilos;
public:
cBigApple() : cApple()
{
fWeightInKilos = 0;
}
cBigApple(int iWC, float fTLoW, float fWiK) : cApple(iWC, fTLoW)
{
fWeightInKilos = fWiK;
}
cBigApple operator -- ()
{
return cBigApple(--iWormCount, --fTotalLengthOfWorms, 0.0);
}
};
class cBigRedApple:public cBigApple
{
public:
cBigRedApple() : cBigApple()
{
}
cBigRedApple(int iWC, float fTLoW, float fWiK) : cBigApple(iWC, fTLoW, fWiK)
{
}
cBigRedApple operator -- ()
{
return cBigRedApple(iWormCount--, fTotalLengthOfWorms--, 0.0);
}
};
int main()
{
cApple oMyApple;
++oMyApple;
cout << "oMyApple:" << endl;
oMyApple.put();
cBigApple oMyBigApple;
++oMyBigApple;
++oMyBigApple;
++oMyBigApple;
cout << "\noMyBigApple:" << endl;
oMyBigApple.put();
cBigRedApple oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
--oMyBigRedApple;
oMyBigRedApple--;
cout << "\noMyBigRedApple:" << endl;
oMyBigRedApple.put();
return 0;
}
|

January 1st, 2006, 09:13 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Vector, but it doesn't quite solve my problem.
What I want is to overload both the prefix and the postfix versions of a unary operator. And I want to do it on different levels in a class / subclass hierarchy. The code you've shown me only overloads the prefix version and the compiler gives me the warning:
"Warn : unaryoverloadvector.cpp(94,21):Overloaded prefix 'operator --' used as a postfix operator"
|

January 1st, 2006, 12:11 PM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The have both the versions of increment operators in your program...
|

January 1st, 2006, 02:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
In the code in my first post I had both
Code:
cBigApple operator -- ()
{
return cBigApple(--iWormCount, --fTotalLengthOfWorms, 0.0);
}
and
Code:
cBigRedApple operator -- (int)
{
return cBigRedApple(iWormCount--, fTotalLengthOfWorms--, 0.0);
}
But the compiler complains that the line in main reading
is an "Error: UnaryOverload.cpp(98,19):Illegal structure operation"
|

January 1st, 2006, 10:23 PM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try making operator-- function of cBigApple a friend function
|

January 2nd, 2006, 05:07 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Vector - the following seems to work:
Code:
/* Overloading and inheritance
** tested using overloads of unary operators
** 051231 Jonax
*/
#include <iostream.h> // cout,cin,endl
#include <conio.h> // getche()
#include <iomanip.h> // setw()
#include <string.h> // strlen()
class cApple
{
protected:
int iWormCount;
float fTotalLengthOfWorms;
public:
cApple()
{
iWormCount = 0;
fTotalLengthOfWorms = 0;
}
cApple(int iWC, float fTLoW)
{
iWormCount = iWC;
fTotalLengthOfWorms = fTLoW;
}
cApple operator ++ ()
{
return cApple(++iWormCount, ++fTotalLengthOfWorms);
}
void put()
{
cout << "Antal orme: " << iWormCount << endl;
cout << "Længde: " << fTotalLengthOfWorms << endl;
}
};
class cBigApple:public cApple
{
private:
float fWeightInKilos;
public:
cBigApple() : cApple()
{
fWeightInKilos = 0;
}
cBigApple(int iWC, float fTLoW, float fWiK) : cApple(iWC, fTLoW)
{
fWeightInKilos = fWiK;
}
friend cBigApple operator -- (cBigApple tmp)
{
return cBigApple(--tmp.iWormCount, --tmp.fTotalLengthOfWorms, 0.0);
}
};
class cBigRedApple:public cBigApple
{
private:
public:
cBigRedApple() : cBigApple()
{
}
cBigRedApple(int iWC, float fTLoW, float fWiK) : cBigApple(iWC, fTLoW, fWiK)
{
}
cBigRedApple operator -- (int)
{
return cBigRedApple(iWormCount--, fTotalLengthOfWorms--, 0.0);
}
};
int main(){
cApple oMyApple;
++oMyApple;
cout << "oMyApple:" << endl;
oMyApple.put();
cBigApple oMyBigApple;
++oMyBigApple;
++oMyBigApple;
++oMyBigApple;
cout << "\noMyBigApple:" << endl;
oMyBigApple.put();
cBigRedApple oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
++oMyBigRedApple;
--oMyBigRedApple;
oMyBigRedApple--;
cout << "\noMyBigRedApple:" << endl;
oMyBigRedApple.put();
getch();
return 0;
}
|

February 7th, 2006, 05:20 PM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Jonax
Thanks Vector - the following seems to work:
Code:
...
friend cBigApple operator -- (cBigApple tmp)
{
return cBigApple(--tmp.iWormCount, --tmp.fTotalLengthOfWorms, 0.0);
}
};
...
|
You might want to make the argument to operator-- a reference to the original object, and return a reference to the object itself. It might look like:
Code:
...
const cBigApple & operator -- (cBigApple & tmp) {
iWormCount -= 1;
fTotalLenthOfWorms -= 1;
return *this;
}
...
This will make sure that the returned object is not a new instance, but rather the same instance with which you called the operator-- with.
This should apply to the other unary operators too.
Hope this helps.
Dean Michael C. Berris
Orange and Bronze Technologies, Inc.
Mobile +639287291459
|

February 27th, 2006, 08:50 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yup...agreed
|
|
 |