operator Overloading in class's

  • 18 years ago

    Hello all....


    ive been learning C++ from an old text book,, copyrighted 1995,,,


    It's made for borland C++ V4.5
    and turbo C++


    however i have found the language to be an exact match to VC++ version 6.0 (what im using)


    HOWEVER...  i have been learing how to use  overloaded operators,
    the binary ones are great, they work fine,,,,


    these are the ones u would use to make the following arithmatic valid



    Class3 = Class1 & class2;


    // the code is sumthing like  Class operator & (class myClass) { code returnung type class; }






    But my book also tells me it is possible to do "Unary" operators,


    Eg,,, int N=0 ; N++  is an examplt of this,, ( an over loaded operator with NO parameters....



    however when i try to implement this the compiler complains


    The Book tells me to do it like so


    void operator ++ () { code altering the class, but returning nothing ; }
    // ERROR : Too Few parameters.




    Can some1 tell me if the syntax for this function has changed over the last 5 or so years, and if so whta is the VC++ way of doing it,,


    or has this usefull function been removed from microsofts compiler ??



    thanX to any1 who could help...


    ALSO, if any1 knows the aproxamate age of borland 4.5 it would help,,, ( i belive it was a DOS program )


    thanX again.













  • 18 years ago

    Hi,


    It fails because u should set the return type, use something like


    [ccode]
    myClass operator++();
    // and u must defined another version of this function
    myClass operator++(int x);
    [/ccode]


    U can use the same implementation for both.


    I hope it helps,
    Dirso.


  • 18 years ago

    it is set to vois because it does not return a a class, it returns nothing.


    eg


    int N=0;
    N++   //increment N


    the abouve does not return anything, only changes the value of N.


    + even if i did change the return type,, as I said, the compiler is complaing about TOO FEW PARAMETERS,,,


    and again we have 0 parameters, so your soultion does not realy help.

  • 18 years ago

    Firstly, i'm just trying to help u, ok?


    Now, let's go...
    1) It MUST return an object... better, it must return the object that u'r incrementing.
    2) I did it under VC6 and it workes well.


    Here is the sample code:


    [ccode]
    class CMyClass  
    {
    public:
       int mN;
       CMyClass() {m
    N = 0;}
       virtual ~CMyClass() {}


       CMyClass operator++()
       {
           m_N++;
           return *this;
       };


       CMyClass operator++(int x)
       {
           m_N++;
           return *this;
       };


    };


    include <stdio.h>



    int main()
    {
       CMyClass c;
       c++;


       printf("%d\n", c.m_N);
       return 0;
    }
    [/ccode]


    I hope it helps, now.


    Best Regards,
    Dirso.

  • 18 years ago

    just a few questions,


    you declaaired the operator to take a parameter int X, yet there is no  parameter,,, why ?


    and second of all, why have you used a virtual destructor ? we dont even need a destructor, yet not only have you made an mpty 1, you made it virtual, seems un necceserily complicated,,, any reason to this ?

  • 18 years ago

    Hi,


    When u uses ++ as a prefix, the compiler calls the operator++(int x), otherwise it calls operator++()


    the virtual destructor is there because i used the ClassWizard to generate my class. No reason.


    Best Regards,
    Dirso

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker