Topic: prefix postfix ++ operator overloading
Author: Andy Sawyer <andys@thone.demon.co.uk>
Date: 1995/05/30 Raw View
In article <3q47nv$hv3@cd4680fs.rrze.uni-erlangen.de>
bess@immd5.informatik.uni-erlangen.de "Ruediger Bess" writes:
> hsc@eng.cam.ac.uk (Hiang-Swee Chiang) writes:
>
> >I have "const CEmuTime& operator ++()" for prefix and "CEmuTime
> >operator ++(int)" for postfix.
> Change the return type either to const CEmuTime& or to CEmuTime for
> BOTH functions and your compiler should not complain any longer.
Chaning the return types as you suggest has the unfortunate effect of
breaking the semantics of the prefix and postfix ++ operators. Unfortunatly,
I missed the original post, but it sounds like the origianl authours compiler
is broken.
Regards,
Andy
--
* Andy Sawyer ** e-mail:andys@thone.demon.co.uk ** Compu$erve:100432,1713 **
The opinions expressed above are my own, but you are granted the right to
use and freely distribute them. I accept no responsibility for any injury,
harm or damage arising from their use. -- The Management.
Author: osinski@valis.cs.nyu.edu (Ed Osinski)
Date: 1995/05/30 Raw View
In article <3q47nv$hv3@cd4680fs.rrze.uni-erlangen.de>, bess@immd5.informatik.uni-erlangen.de (Ruediger Bess) writes:
|> hsc@eng.cam.ac.uk (Hiang-Swee Chiang) writes:
|>
|> >I have "const CEmuTime& operator ++()" for prefix and "CEmuTime
|> >operator ++(int)" for postfix.
|> Change the return type either to const CEmuTime& or to CEmuTime for
|> BOTH functions and your compiler should not complain any longer.
Huh? Maybe I'm missing some relevant context from the original article,
but it certainly is not necessary to delare both prefix and postfix '++' to
have the same return type.
--
---------------------------------------------------------------------
Ed Osinski
Computer Science Department, New York University
E-mail: osinski@cs.nyu.edu
---------------------------------------------------------------------
"No, no, no, don't tug on that. You never know what it might be attached to."
-- Buckaroo Bonzai to assistant during brain surgery
Author: bess@immd5.informatik.uni-erlangen.de (Ruediger Bess)
Date: 1995/05/26 Raw View
hsc@eng.cam.ac.uk (Hiang-Swee Chiang) writes:
>I have "const CEmuTime& operator ++()" for prefix and "CEmuTime
>operator ++(int)" for postfix.
Change the return type either to const CEmuTime& or to CEmuTime for
BOTH functions and your compiler should not complain any longer.
------------------------------------------------------------------------
Ruediger Bess Universitaet Erlangen-Nuernberg
Lehrstuhl fuer Mustererkennung (Informatik 5) (Prof. Dr. H. Niemann)
Martensstr. 3 D-91058 Erlangen Germany
Tel. +9131-857891 / 857774 FAX: +9131-303811
email: bess@informatik.uni-erlangen.de
------------------------------------------------------------------------
Author: hsc@eng.cam.ac.uk (Hiang-Swee Chiang)
Date: 1995/05/24 Raw View
Hello Dear C++ Developer,
I have created a base class called CEmuFloat that emulate floating point
operation using integer operation. For obvious reason, I defined the prefix
and postfix ++ operator for this CEmuFloat class.
Next, I created a derived class called CEmuTime from CEmuFloat. In this
derived class I wanted to overload the base class prefix and postfix ++
operator. So I have "const CEmuTime& operator ++()" for prefix and "CEmuTime
operator ++(int)" for postfix.
However, my compiler complained that overload operator cannot differ only by
return type which is true. Is there a way to overcome this problem?
I resort to using friend function which worked on Microsoft Visual C++ 2.0 but
my gcc cross-compiler for the ARM chip complaints that it is ambiguous and
refused to compile. Therefore I have to resort to CEmuTime::PrefixAddOne()
and CEmuTime::PostfixAddOne() member functions which are not elegant. I just
don't believe that C++ does not have any work around to this simple problem.
Someone please enlighten me.
Regards,
Swee
Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 1995/05/24 Raw View
hsc@eng.cam.ac.uk (Hiang-Swee Chiang) writes:
>Hello Dear C++ Developer,
>I have created a base class called CEmuFloat that emulate floating point
>operation using integer operation. For obvious reason, I defined the prefix
>and postfix ++ operator for this CEmuFloat class.
>Next, I created a derived class called CEmuTime from CEmuFloat. In this
>derived class I wanted to overload the base class prefix and postfix ++
>operator. So I have "const CEmuTime& operator ++()" for prefix and "CEmuTime
>operator ++(int)" for postfix.
>However, my compiler complained that overload operator cannot differ only by
>return type which is true. Is there a way to overcome this problem?
>I resort to using friend function which worked on Microsoft Visual C++ 2.0 but
>my gcc cross-compiler for the ARM chip complaints that it is ambiguous and
>refused to compile. Therefore I have to resort to CEmuTime::PrefixAddOne()
>and CEmuTime::PostfixAddOne() member functions which are not elegant. I just
>don't believe that C++ does not have any work around to this simple problem.
First of all, though you did not say it, I assume that you made all
the member functions virtual. If you are getting the error messages,
you described, for non-virtual functions, there is something wrong
with your compiler.
Second, your compiler may not be up to date in terms of standard
compliance. Changing return types of overloaded virtual functions is
a relatively recent addition to the standard.
Third, the changing of return types for virtual functions, as defined
in the proposed standard, is quite restricted. The virtual function
in the base class must return a pointer or a reference to some class
(possibly with const and/or volatile), and the overriding function
must return a (coresponding) pointer or reference to a class derived
from the class reurned by the base class function. There are a few
other restrictions. This means that you will have problems with the
postfix forms of ++ and --, and might wish to eliminate them.
Good Luck!