🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
14 messages
14 total messages Started by Christopher Deli Thu, 16 May 2002 21:17
virtual operator+
#79
Author: Christopher Deli
Date: Thu, 16 May 2002 21:17
17 lines
1808 bytes
Hello!

I have a problem which I'd need some help with, please.
If I have one class A which is inherited by another class B i.e. class B :
public class A and I would like to make a virtual operator+ in both class A
and B so I could do the following:
A *a, *b, *c
a = new B(...)
b = new B(...)
c = new B(...)
*c = *a + *b
So I make a pointer of type A which points on an objekt of type B. For that
I'd need a virtual operator+ but I have no idea how to make it. Please help!
The source code of a program which has the similar situation would be
perfekt! Thanks!

// Christopher
Re: virtual operator+
#80
Author: Daniel T.
Date: Thu, 16 May 2002 21:31
23 lines
1993 bytes
"Christopher Deli" <christopher@delinfo.com> wrote:

>Hello!
>
>I have a problem which I'd need some help with, please.
>If I have one class A which is inherited by another class B i.e. class B :
>public class A and I would like to make a virtual operator+ in both class A
>and B so I could do the following:
>A *a, *b, *c
>a = new B(...)
>b = new B(...)
>c = new B(...)
>*c = *a + *b
>So I make a pointer of type A which points on an objekt of type B. For that
>I'd need a virtual operator+ but I have no idea how to make it. Please help!
>The source code of a program which has the similar situation would be
>perfekt! Thanks!

What is op+ supposed to return, an "A" object or a "B" object?

-- 
Improve your company's understanding of objects...
Hire me. <http://home1.gte.net/danielt3/resume.html>
Re: virtual operator+
#81
Author: Christopher Deli
Date: Thu, 16 May 2002 22:56
31 lines
2409 bytes
a B object. Thanks.

"Daniel T." <notdanielt3@gte.net> skrev i meddelandet
news:notdanielt3-34A906.17313116052002@news.bellatlantic.net...
> "Christopher Deli" <christopher@delinfo.com> wrote:
>
> >Hello!
> >
> >I have a problem which I'd need some help with, please.
> >If I have one class A which is inherited by another class B i.e. class B
:
> >public class A and I would like to make a virtual operator+ in both class
A
> >and B so I could do the following:
> >A *a, *b, *c
> >a = new B(...)
> >b = new B(...)
> >c = new B(...)
> >*c = *a + *b
> >So I make a pointer of type A which points on an objekt of type B. For
that
> >I'd need a virtual operator+ but I have no idea how to make it. Please
help!
> >The source code of a program which has the similar situation would be
> >perfekt! Thanks!
>
> What is op+ supposed to return, an "A" object or a "B" object?
>
> --
> Improve your company's understanding of objects...
> Hire me. <http://home1.gte.net/danielt3/resume.html>
Re: virtual operator+
#82
Author: Robert W Hand
Date: Thu, 16 May 2002 23:23
20 lines
2251 bytes
On Thu, 16 May 2002 21:31:31 GMT, "Daniel T." <notdanielt3@gte.net>
wrote:

>"Christopher Deli" <christopher@delinfo.com> wrote:
>
>>Hello!
>>
>>I have a problem which I'd need some help with, please.
>>If I have one class A which is inherited by another class B i.e. class B :
>>public class A and I would like to make a virtual operator+ in both class A

>What is op+ supposed to return, an "A" object or a "B" object?

Aren't the two return types A and B covariant?  The one in the A class
would return an A object, and the one in the B class would return a B
object.  

Best wishes,

Bob
Re: virtual operator+
#83
Author: Victor Bazarov
Date: Thu, 16 May 2002 23:30
27 lines
2191 bytes
"Robert W Hand" <rwhand@NOSPAMoperamail.com> wrote...
> On Thu, 16 May 2002 21:31:31 GMT, "Daniel T." <notdanielt3@gte.net>
> wrote:
>
> >"Christopher Deli" <christopher@delinfo.com> wrote:
> >
> >>Hello!
> >>
> >>I have a problem which I'd need some help with, please.
> >>If I have one class A which is inherited by another class B i.e. class B
:
> >>public class A and I would like to make a virtual operator+ in both
class A
>
> >What is op+ supposed to return, an "A" object or a "B" object?
>
> Aren't the two return types A and B covariant?  The one in the A class
> would return an A object, and the one in the B class would return a B
> object.


No.  Covariance requires a pointer or a reference.  Operator +
returns an object, not a pointer and not a reference.

Victor
--
Please remove capital A's from my address when replying by mail
Re: virtual operator+
#84
Author: Victor Bazarov
Date: Thu, 16 May 2002 23:34
12 lines
1755 bytes
"Christopher Deli" <christopher@delinfo.com> wrote...
> a B object. Thanks.


First, please don't top-post.  Second, how should a base class'
operator + return an object of derived class?  And if it doesn't,
then the derived class' operator will not be overriding the base
class' operator -- the signatures have to match.

Victor
--
Please remove capital A's from my address when replying by mail
Re: virtual operator+
#85
Author: Josh Sebastian
Date: Fri, 17 May 2002 05:13
24 lines
2059 bytes
On Thu, 16 May 2002 21:17:53 GMT, "Christopher Deli"
<christopher@delinfo.com> wrote:

>Hello!
>
>I have a problem which I'd need some help with, please.
>If I have one class A which is inherited by another class B i.e. class B :
>public class A and I would like to make a virtual operator+ in both class A
>and B so I could do the following:
>A *a, *b, *c
>a = new B(...)
>b = new B(...)
>c = new B(...)
>*c = *a + *b
>So I make a pointer of type A which points on an objekt of type B. For that
>I'd need a virtual operator+ but I have no idea how to make it. Please help!
>The source code of a program which has the similar situation would be
>perfekt! Thanks!

If you did this, then *a + *b could mean something (very) different
from *b + *a. That's not a good idea, generally. Perhaps you shouldn't
use an overloaded operator, in this circumstance.

Josh
Re: virtual operator+
#86
Author: Christopher Deli
Date: Fri, 17 May 2002 07:59
39 lines
2850 bytes
The thing is that the assignment I am working with demands that I have a
base class A with two subclasses (B and C) and I need to make ONE list with
both B and C objects. A should be an abstract class. So I need to create an
A pointer which sometimes points on B objects and sometimes on C objects.
And on top of that I need an operator which can add two B objects. If I only
put the operator in class B it wont work if the pointers I try to add have
static type A and dynamic type B.

// Christopher
"Josh Sebastian" <curien@cox.net> skrev i meddelandet
news:rg49eusfujq9l0nq0m5dimtbenu36m0lor@4ax.com...
> On Thu, 16 May 2002 21:17:53 GMT, "Christopher Deli"
> <christopher@delinfo.com> wrote:
>
> >Hello!
> >
> >I have a problem which I'd need some help with, please.
> >If I have one class A which is inherited by another class B i.e. class B
:
> >public class A and I would like to make a virtual operator+ in both class
A
> >and B so I could do the following:
> >A *a, *b, *c
> >a = new B(...)
> >b = new B(...)
> >c = new B(...)
> >*c = *a + *b
> >So I make a pointer of type A which points on an objekt of type B. For
that
> >I'd need a virtual operator+ but I have no idea how to make it. Please
help!
> >The source code of a program which has the similar situation would be
> >perfekt! Thanks!
>
> If you did this, then *a + *b could mean something (very) different
> from *b + *a. That's not a good idea, generally. Perhaps you shouldn't
> use an overloaded operator, in this circumstance.
>
> Josh
Re: virtual operator+
#87
Author: Karl Heinz Buchegger
Date: Fri, 17 May 2002 08:40
31 lines
2407 bytes

Christopher Deli wrote:

> The thing is that the assignment I am working with demands that I have a
> base class A with two subclasses (B and C) and I need to make ONE list with
> both B and C objects. A should be an abstract class. So I need to create an
> A pointer which sometimes points on B objects and sometimes on C objects.
> And on top of that I need an operator which can add two B objects. If I only
> put the operator in class B it wont work if the pointers I try to add have
> static type A and dynamic type B.
> 


First of all: please don't top post.
Scroll to the section you want to reply to, and insert
your comments there.

As you have seen, there is a technical problem with having
an operator: the return type.
Are you sure you *must* implement an operator or will an
ordinary member function be sufficient?
If your assignment explicitely states an operator, then I
would suggest to ask your teacher, how he thinks this should
work (Hint: it is impossible, since operator+ has to return
an object, but for polymorphism you would need a pointer or
a reference).

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at
Re: virtual operator+
#88
Author: Karl Heinz Buchegger
Date: Fri, 17 May 2002 08:46
44 lines
2677 bytes

Karl Heinz Buchegger wrote:

> 
> 
> Christopher Deli wrote:
> 
>> The thing is that the assignment I am working with demands that I have a
>> base class A with two subclasses (B and C) and I need to make ONE list 
>> with
>> both B and C objects. A should be an abstract class. So I need to 
>> create an
>> A pointer which sometimes points on B objects and sometimes on C objects.
>> And on top of that I need an operator which can add two B objects. If 
>> I only
>> put the operator in class B it wont work if the pointers I try to add 
>> have
>> static type A and dynamic type B.
>>
> 
> 
> First of all: please don't top post.
> Scroll to the section you want to reply to, and insert
> your comments there.
> 
> As you have seen, there is a technical problem with having
> an operator: the return type.
> Are you sure you *must* implement an operator or will an
> ordinary member function be sufficient?
> If your assignment explicitely states an operator, then I
> would suggest to ask your teacher, how he thinks this should
> work (Hint: it is impossible, since operator+ has to return
> an object, but for polymorphism you would need a pointer or
> a reference).
> 

I should have said: It is impossible without giving the operator+
a very unusual semantics. Something you definitly want to avoid.


-- 
Karl Heinz Buchegger
kbuchegg@gascad.at
Re: virtual operator+
#89
Author: Christopher Deli
Date: Fri, 17 May 2002 08:51
47 lines
3009 bytes
"Karl Heinz Buchegger" <kbuchegg@gascad.at> skrev i meddelandet
news:3CE4C209.7010606@gascad.at...
>
>
> Christopher Deli wrote:
>
> > The thing is that the assignment I am working with demands that I have a
> > base class A with two subclasses (B and C) and I need to make ONE list
with
> > both B and C objects. A should be an abstract class. So I need to create
an
> > A pointer which sometimes points on B objects and sometimes on C
objects.
> > And on top of that I need an operator which can add two B objects. If I
only
> > put the operator in class B it wont work if the pointers I try to add
have
> > static type A and dynamic type B.
> >
>
>
> First of all: please don't top post.
> Scroll to the section you want to reply to, and insert
> your comments there.
>
> As you have seen, there is a technical problem with having
> an operator: the return type.
> Are you sure you *must* implement an operator or will an
> ordinary member function be sufficient?
> If your assignment explicitely states an operator, then I
> would suggest to ask your teacher, how he thinks this should
> work (Hint: it is impossible, since operator+ has to return
> an object, but for polymorphism you would need a pointer or
> a reference).
>
> --
> Karl Heinz Buchegger
> kbuchegg@gascad.at
>

Can I create a member function which serves the same function as an operator
(is it possible to write a = c + b?)? I tried to make a friend operator
aswell but the same things happened.. Isn't it possible to change the static
type of my pointer by type casting or something so it could be enough with
and operator in the subclass?

//Christopher
Re: virtual operator+
#90
Author: Karl Heinz Buchegger
Date: Fri, 17 May 2002 09:11
89 lines
3588 bytes

Christopher Deli wrote:


>>As you have seen, there is a technical problem with having
>>an operator: the return type.
>>Are you sure you *must* implement an operator or will an
>>ordinary member function be sufficient?
>>If your assignment explicitely states an operator, then I
>>would suggest to ask your teacher, how he thinks this should
>>work (Hint: it is impossible, since operator+ has to return
>>an object, but for polymorphism you would need a pointer or
>>a reference).
>>
>>--
>>Karl Heinz Buchegger
>>kbuchegg@gascad.at
>>
>>
> 
> Can I create a member function which serves the same function as an operator

> (is it possible to write a = c + b?)?


No. Thats what operators are for.
Your problem is not the + syntax. Your problem is that this functions has
to return an object and this object has to be of a specific type.

  * Either define the return type to be a base class type

       class A
       {
          A virtual operator+( const A& Arg );
       };

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

    But this will not work. At the moment that class B's operator+
    returns the result, which would be of type B, it will be sliced
    into an A object (it will loose all its B information, just the A
    part remains).

  * Don't make them virtual

    class A
    {
      A operator+( const A& Arg );
    };

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

    But since they are not virtual, polymorphism will not work.

  * Prepare base class A, that there will be overloads:

    class B;
    class A
    {
      A virtual operator+( const A& Arg );
      B virtual operator+( const B& Arg );
    };

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

    Ugly. Why should class A know, that someone is inheriting from it?

> I tried to make a friend operator
> aswell but the same things happened.. Isn't it possible to change the static
> type of my pointer by type casting or something so it could be enough with
> and operator in the subclass?


look up dynamic_cast.
It may help you in this case.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at
Re: virtual operator+
#91
Author: Robert W Hand
Date: Fri, 17 May 2002 12:02
19 lines
2299 bytes
On Thu, 16 May 2002 23:30:03 GMT, "Victor Bazarov"
<vAbazarov@dAnai.com> wrote:
>
>No.  Covariance requires a pointer or a reference.  Operator +
>returns an object, not a pointer and not a reference.
>

Thanks for the information; I had forgotten the correct definition.
Then his request is impossible as stated.  He'll be hung up on the
return type.  He could define a virtual member function like:

virtual void addition(const B& operand, B& result) const;

He will need a dynamic cast in the body of the overriding function.
Not very pretty.  :-(

Best wishes,

Bob
Re: virtual operator+
#92
Author: Daniel T.
Date: Fri, 17 May 2002 12:09
48 lines
2627 bytes
"Christopher Deli" <christopher@delinfo.com> wrote:

>The thing is that the assignment I am working with demands that I have a 
>base class A with two subclasses (B and C) and I need to make ONE list 
>with both B and C objects. A should be an abstract class. So I need to 
>create an A pointer which sometimes points on B objects and sometimes on C 
>objects. And on top of that I need an operator which can add two B 
>objects. If I only put the operator in class B it wont work if the 
>pointers I try to add have static type A and dynamic type B.

You want this?

int main() {
    A* var1 = new B;
    A* var2 = new B;
    B result_b = *var1 + *var2;

    delete var1;
    delete var2;

    var1 = new C;
    var2 = new C;
    C result_c = *var1 + *var2;
}

If so, what should happen if var1 is of dynamic type B and var2 is of 
dynamic type C? (or vise-versa?)

Or do you want this?

int main() {
    A* var1 = new B;
    A* var2 = new B;
    A result = *var1 + *var2;

    delete var1;
    delete var2;

    var1 = new C;
    var2 = new C;
    result = *var1 + *var2;
}

If so, please understand that your result will be loosing information. 
An 'A' object cannot hold all the information that a B or C object can.

Are you sure you have to be able to add two dereferenced A* objects 
together? Maybe you only need to be able to add two B objects.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads