From: jamaj on

Dear All,

I have a class named DataElem, with some specialized derived classes
DataFloatElem, DataIntElem and so on.

I want to create a operator (possibly a virtual operator += on the
DataElem), that would be able to operate with the others.

So, DataFloatElem(1.0) += DataIntElem(1) should give a
DataFloatElem(2.0) as result.

Is there any way to do so?

Thanks in advance.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Jul 11, 4:46 pm, jamaj <jama...(a)gmail.com> wrote:
> Dear All,
>
> I have a class named DataElem, with some specialized derived classes
> DataFloatElem, DataIntElem and so on.
>
> I want to create a operator (possibly a virtual operator += on the
> DataElem), that would be able to operate with the others.
>
> So, DataFloatElem(1.0) += DataIntElem(1) should give a
> DataFloatElem(2.0) as result.
>
> Is there any way to do so?

I presume that you also want that DataIntElem(1) + DataIntElem(1) give
out DataIntElem(2), right? And all that through

virtual DataIntElem& operator+=(const DataIntElem& rhs)

?

Things already work to your favor in these two cases.

However, DataIntElem(1) += DataFloatElem(1.0) does not normally work -
one would prefer ...Float... result, but you'd get ...Int... (that's
old ...Int..., but with it's new value).

Look at how operator += is "declared" by the language (e.g. in
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Compound-assignment_operators).
I would be wary of breaking "standard" signature of any operator, and
I would not do what you're trying to do. That includes going virtual
with overloaded operators; in fact, I tried that a couple of times
over the years, and it never pan out well for me.

So thanks, your attempt just reminded me why virtual operator
overloads are a bad idea ;-).

Goran.

P.S. BTW, in raw C, these things work with primitive types (e.g. int,
float) through implicit conversions, not through operator
overloading ;-) Perhaps you should try that instead, but then again,
I'd be wary.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: cpp4ever on
On 07/12/2010 02:15 PM, Goran wrote:
> On Jul 11, 4:46 pm, jamaj <jama...(a)gmail.com> wrote:
>> Dear All,
>>
>> I have a class named DataElem, with some specialized derived classes
>> DataFloatElem, DataIntElem and so on.
>>
>> I want to create a operator (possibly a virtual operator += on the
>> DataElem), that would be able to operate with the others.
>>
>> So, DataFloatElem(1.0) += DataIntElem(1) should give a
>> DataFloatElem(2.0) as result.
>>
>> Is there any way to do so?
>
> I presume that you also want that DataIntElem(1) + DataIntElem(1) give
> out DataIntElem(2), right? And all that through
>
> virtual DataIntElem& operator+=(const DataIntElem& rhs)
>
> ?
>
> Things already work to your favor in these two cases.
>
> However, DataIntElem(1) += DataFloatElem(1.0) does not normally work -
> one would prefer ...Float... result, but you'd get ...Int... (that's
> old ...Int..., but with it's new value).
>
> Look at how operator += is "declared" by the language (e.g. in
> http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Compound-assignment_operators).
> I would be wary of breaking "standard" signature of any operator, and
> I would not do what you're trying to do. That includes going virtual
> with overloaded operators; in fact, I tried that a couple of times
> over the years, and it never pan out well for me.
>
> So thanks, your attempt just reminded me why virtual operator
> overloads are a bad idea ;-).
>
> Goran.
>
> P.S. BTW, in raw C, these things work with primitive types (e.g. int,
> float) through implicit conversions, not through operator
> overloading ;-) Perhaps you should try that instead, but then again,
> I'd be wary.
>
>

Without careful design, I'd agree, but in this case I would define the
operator += () in a standard form as follows:


class A
{
public:
virtual const A &operator += (const A&);
}

class B : public A
{
public:
virtual const B &operator += (const A &);
const B &operator += (const B &)
}

B operator + (const A &inp1, const B &inp2)
{
B tmp = inp2;
tmp += inp1;
return tmp;
}

So for any class operator +=() will always returns a const reference to
the class object that handled it. Whereas for operator +() the simplest
solution is to return by value the most appropriate class type.

HTH

cpp4ever

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Jul 13, 12:27 am, cpp4ever <n2xssvv.g02gfr12...(a)ntlworld.com>
wrote:
> Without careful design, I'd agree, but in this case I would define the
> operator += () in a standard form as follows:
>
> class A
> {
> public:
> virtual const A &operator += (const A&);
>
> }
>
> class B : public A
> {
> public:
> virtual const B &operator += (const A &);
> const B &operator += (const B &)
>
> }


Well, yes, but that's IMO not good enough for OP: in b+=a, where b is
"Int" and a is "Float", then calculation should result in "Float",
which is not possible for "standard" (well, "close-to-standard") form
of operator +=.

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: cpp4ever on
On 07/13/2010 09:45 AM, Goran wrote:
> On Jul 13, 12:27 am, cpp4ever <n2xssvv.g02gfr12...(a)ntlworld.com>
> wrote:
>> Without careful design, I'd agree, but in this case I would define the
>> operator += () in a standard form as follows:
>>
>> class A
>> {
>> public:
>> virtual const A &operator += (const A&);
>>
>> }
>>
>> class B : public A
>> {
>> public:
>> virtual const B &operator += (const A &);
>> const B &operator += (const B &)
>>
>> }
>
>
> Well, yes, but that's IMO not good enough for OP: in b+=a, where b is
> "Int" and a is "Float", then calculation should result in "Float",
> which is not possible for "standard" (well, "close-to-standard") form
> of operator +=.
>
> Goran.
>
>

In that type of case case you would have to cast the float to an int
before it could be assigned, and the same should apply for classes.
Hence if no cast operator or constructor is available to do just that a
compile error will occur.

In your example b=a the result should an integer and not a float, (the
float first being cast to an integer and should cause a compile
warning), if you then require the answer as a float you could do the
following

b=a; // b is int, a is float
c=b; // c is float, b is int

Bottom line is the result of any expression needs to be cast to the type
it is going to be assigned to!!!

IMO to propose anything different is foolhardy.

HTH

cpp4ever

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]