Article View: alt.comp.lang.learn.c-c++
Article #90Re: virtual operator+
From: Karl Heinz Buchegger
Date: Fri, 17 May 2002 09:11
Date: Fri, 17 May 2002 09:11
89 lines
3588 bytes
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
Technical Details
Message-ID:
<3CE4C956.5070601@gascad.at>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!130.133.1.3!fu-berlin.de!uni-berlin.de!62.46.208.145!not-for-mailReferences:
<5mVE8.38287$n4.8427350@newsc.telia.net> <rg49eusfujq9l0nq0m5dimtbenu36m0lor@4ax.com> <VL2F8.38343$n4.8444053@newsc.telia.net> <3CE4C209.7010606@gascad.at> <6w3F8.38349$n4.8445243@newsc.telia.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d4ba,e2ff40a616ce87ab X-Google-Attributes: gid10d4ba,public X-Google-Thread: 109fba,e2ff40a616ce87ab X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,e2ff40a616ce87ab X-Google-Attributes: gid114592,public X-Google-ArrivalTime: 2002-05-17 02:07:56 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!130.133.1.3!fu-berlin.de!uni-berlin.de!62.46.208.145!not-for-mail From: Karl Heinz Buchegger <kbuchegg@gascad.at> Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++ Subject: Re: virtual operator+ Date: Fri, 17 May 2002 11:11:50 +0200 Organization: GASCAD Lines: 90 Message-ID: <3CE4C956.5070601@gascad.at> References: <5mVE8.38287$n4.8427350@newsc.telia.net> <rg49eusfujq9l0nq0m5dimtbenu36m0lor@4ax.com> <VL2F8.38343$n4.8444053@newsc.telia.net> <3CE4C209.7010606@gascad.at> <6w3F8.38349$n4.8445243@newsc.telia.net> NNTP-Posting-Host: 62.46.208.145 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: fu-berlin.de 1021626475 23505867 62.46.208.145 (16 [95537]) User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:0.9.4.1) Gecko/20020314 Netscape6/6.2.2 X-Accept-Language: de-DE