Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 1st, 2006, 12:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default 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;
}



Reply With Quote
  #2 (permalink)  
Old January 1st, 2006, 02:34 AM
Authorized User
 
Join Date: Nov 2005
Location: , , India.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #3 (permalink)  
Old January 1st, 2006, 09:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

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"


Reply With Quote
  #4 (permalink)  
Old January 1st, 2006, 12:11 PM
Authorized User
 
Join Date: Nov 2005
Location: , , India.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The have both the versions of increment operators in your program...

Reply With Quote
  #5 (permalink)  
Old January 1st, 2006, 02:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

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
Code:
    --oMyBigRedApple;
is an "Error: UnaryOverload.cpp(98,19):Illegal structure operation"


Reply With Quote
  #6 (permalink)  
Old January 1st, 2006, 10:23 PM
Authorized User
 
Join Date: Nov 2005
Location: , , India.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try making operator-- function of cBigApple a friend function

Reply With Quote
  #7 (permalink)  
Old January 2nd, 2006, 05:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

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;
}
Reply With Quote
  #8 (permalink)  
Old February 7th, 2006, 05:20 PM
Registered User
 
Join Date: Feb 2006
Location: , , Philippines.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mikhailberis
Default

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
Reply With Quote
  #9 (permalink)  
Old February 27th, 2006, 08:50 AM
Authorized User
 
Join Date: Nov 2005
Location: , , India.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yup...agreed

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
Overload for specialization? Raconteur Visual Basic 2005 Basics 2 May 7th, 2007 10:49 AM
Operator Overloading In Inheritance ahmedsalam C# 0 October 18th, 2005 11:25 AM
subtraction operator overload arnie6 C++ Programming 0 April 19th, 2005 07:57 AM
can we overload 'as' keyword g_natarajan_mca General .NET 0 August 30th, 2004 07:42 AM



All times are GMT -4. The time now is 02:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.