Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual operator +

Reply
Thread Tools

virtual operator +

 
 
Hunk
Guest
Posts: n/a
 
      09-11-2007
Hi

I ws wondering if there is a way to implement operator+ in case of
virtual classes.
Here's the problem. I have to have a base string class from which two
classes (normal char string and a hash string class ) are derived. The
two derived classes are template classes specifying the sizes. The
base class is a non-template class so that it can be used generically
in the interface classes. the design would look like

class Base_string {
};

template<size>
class Char_string : Base_string {
};

template<size>
class Hash_string: Base_string{
};
So that in the interface class of the application he can use just the
generic Base_string to access the functions and doesnt have to know
whether its a Char or hash string
The issue is in implementing the operator+ . Since all the methods are
virtual in the base class and it should call the desired methods
polymorphically, operator+ is a challenge as it returns a Base_string
object
So if I have something like
Char_string<24> char_string1("Hello");
Char_string<24> char_string2("world");
Char_string<24> char_result;
Base_string* base_a = &char_string1;
Base_string* base_b = &char_string2;
Base_string* base_r = &char_result;

i wouldnt be able to do
*base_r = *base_a + *base_b; as the operator+ would return a
Base_object?
Any soultions to the above issue is most welcome

 
Reply With Quote
 
 
 
 
year1943@front.ru
Guest
Posts: n/a
 
      09-11-2007
On 11 , 18:17, Hunk <(E-Mail Removed)> wrote:
> Hi


> I ws wondering if there is a way to implement operator+ in case of
> virtual classes.
> Char_string<24> char_string1("Hello");
> Char_string<24> char_string2("world");
> Char_string<24> char_result;
> Base_string* base_a = &char_string1;
> Base_string* base_b = &char_string2;
> Base_string* base_r = &char_result;


> i wouldnt be able to do
> *base_r = *base_a + *base_b; as the operator+ would return a
> Base_object?
> Any soultions to the above issue is most welcome

If you call operator + in context where compiler doesn't know the real
(dynamic, runtime) types of lhs & rhs objects of op+, it of course
would expect that return type of op+ is as specified in operator + for
used static types of lhs, rhs.
Example: for (Base*, Base*), if op+ returns Base, compiler expect
Base, and reject you to assign Base to Derived, as usual.
If you sure your Base-s in this context would always be pointers/
references to real (dynamic, runtime) Derived objects, you may
dynamic_cast<Derived>(result_of_op_plus) and assign. But this is you
work of'course, compiler shouldn't allow you convert Base to Derived
by default, automatically.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-11-2007
Hunk wrote:
> I ws wondering if there is a way to implement operator+ in case of
> virtual classes.
> Here's the problem. I have to have a base string class from which two
> classes (normal char string and a hash string class ) are derived. The
> two derived classes are template classes specifying the sizes. The
> base class is a non-template class so that it can be used generically
> in the interface classes. the design would look like
>
> class Base_string {
> };
>
> template<size>
> class Char_string : Base_string {
> };
>
> template<size>
> class Hash_string: Base_string{
> };
> So that in the interface class of the application he can use just the
> generic Base_string to access the functions and doesnt have to know
> whether its a Char or hash string
> The issue is in implementing the operator+ . Since all the methods are
> virtual in the base class and it should call the desired methods
> polymorphically, operator+ is a challenge as it returns a Base_string
> object
> So if I have something like
> Char_string<24> char_string1("Hello");
> Char_string<24> char_string2("world");
> Char_string<24> char_result;
> Base_string* base_a = &char_string1;
> Base_string* base_b = &char_string2;
> Base_string* base_r = &char_result;
>
> i wouldnt be able to do
> *base_r = *base_a + *base_b; as the operator+ would return a
> Base_object?
> Any soultions to the above issue is most welcome


Don't think much of overriding the operator+. Let it live in the base
class, and let it return the Base_string. Overload the _assignment_
operator in each of the derived classes:

template<size>
class Char_string : Base_string {
Char_string& operator=(Base_string const&) {
// do what's needed
return *this;
}
};

template<size>
class Hash_string: Base_string {

Hash_string& operator=(Base_string const&) {
// do what's needed
return *this;
}
};

That way you can assign the result of the operator+ to the correct
object. And the proper operator= function will be called. Do in it
what you have to.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
year1943@front.ru
Guest
Posts: n/a
 
      09-11-2007
Also note, op+ for (Base&, Base&) should really return Derived type -
so, if you want to have not only one
Derived operator +(Base&, Base&)
that always return Derived for any given Base*s, but family of
Base* operator +(Base&, Base&)
that returns Derived only for Derived args, op should return result
not by value...

 
Reply With Quote
 
Hunk
Guest
Posts: n/a
 
      09-11-2007
On Sep 11, 7:52 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Hunk wrote:
> > I ws wondering if there is a way to implement operator+ in case of
> > virtual classes.
> > Here's the problem. I have to have a base string class from which two
> > classes (normal char string and a hash string class ) are derived. The
> > two derived classes are template classes specifying the sizes. The
> > base class is a non-template class so that it can be used generically
> > in the interface classes. the design would look like

>
> > class Base_string {
> > };

>
> > template<size>
> > class Char_string : Base_string {
> > };

>
> > template<size>
> > class Hash_string: Base_string{
> > };
> > So that in the interface class of the application he can use just the
> > generic Base_string to access the functions and doesnt have to know
> > whether its a Char or hash string
> > The issue is in implementing the operator+ . Since all the methods are
> > virtual in the base class and it should call the desired methods
> > polymorphically, operator+ is a challenge as it returns a Base_string
> > object
> > So if I have something like
> > Char_string<24> char_string1("Hello");
> > Char_string<24> char_string2("world");
> > Char_string<24> char_result;
> > Base_string* base_a = &char_string1;
> > Base_string* base_b = &char_string2;
> > Base_string* base_r = &char_result;

>
> > i wouldnt be able to do
> > *base_r = *base_a + *base_b; as the operator+ would return a
> > Base_object?
> > Any soultions to the above issue is most welcome

>
> Don't think much of overriding the operator+. Let it live in the base
> class, and let it return the Base_string. Overload the _assignment_
> operator in each of the derived classes:
>

If operator+ lives in the base class it would lead to errors.
For eg implementation for operator+ would look like

Base_string operator + (const Base_string& p_string_r) const
{
Base_string temp_str = *this;
temp_str.append(p_string_r.get_string()); //
return temp_str;
}
The problem with this is , the get_string and append are all virtual
in the base class... they would be overridden in the derived class...
for eg get_string for base class is meaningless as it does not contain
any data. So this would bomb out here itself. So am not sure this idea
would work.
> template<size>
> class Char_string : Base_string {
> Char_string& operator=(Base_string const&) {
> // do what's needed
> return *this;
> }
> };
>
> template<size>
> class Hash_string: Base_string {
>
> Hash_string& operator=(Base_string const&) {
> // do what's needed
> return *this;
> }
> };
>
> That way you can assign the result of the operator+ to the correct
> object. And the proper operator= function will be called. Do in it
> what you have to.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      09-11-2007
Hunk wrote:
> On Sep 11, 7:52 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Hunk wrote:
>>> I ws wondering if there is a way to implement operator+ in case of
>>> virtual classes.
>>> Here's the problem. I have to have a base string class from which two
>>> classes (normal char string and a hash string class ) are derived. The
>>> two derived classes are template classes specifying the sizes. The
>>> base class is a non-template class so that it can be used generically
>>> in the interface classes. the design would look like
>>> class Base_string {
>>> };
>>> template<size>
>>> class Char_string : Base_string {
>>> };
>>> template<size>
>>> class Hash_string: Base_string{
>>> };
>>> So that in the interface class of the application he can use just the
>>> generic Base_string to access the functions and doesnt have to know
>>> whether its a Char or hash string
>>> The issue is in implementing the operator+ . Since all the methods are
>>> virtual in the base class and it should call the desired methods
>>> polymorphically, operator+ is a challenge as it returns a Base_string
>>> object
>>> So if I have something like
>>> Char_string<24> char_string1("Hello");
>>> Char_string<24> char_string2("world");
>>> Char_string<24> char_result;
>>> Base_string* base_a = &char_string1;
>>> Base_string* base_b = &char_string2;
>>> Base_string* base_r = &char_result;
>>> i wouldnt be able to do
>>> *base_r = *base_a + *base_b; as the operator+ would return a
>>> Base_object?
>>> Any soultions to the above issue is most welcome

>> Don't think much of overriding the operator+. Let it live in the base
>> class, and let it return the Base_string. Overload the _assignment_
>> operator in each of the derived classes:
>>

> If operator+ lives in the base class it would lead to errors.
> For eg implementation for operator+ would look like
>
> Base_string operator + (const Base_string& p_string_r) const
> {
> Base_string temp_str = *this;
> temp_str.append(p_string_r.get_string()); //
> return temp_str;
> }
> The problem with this is , the get_string and append are all virtual
> in the base class... they would be overridden in the derived class...
> for eg get_string for base class is meaningless as it does not contain
> any data. So this would bomb out here itself. So am not sure this idea
> would work.


I can imagine that in some cases what you are saying would be true, it
isn't a given. It is quite common to have a base class function call
various pure virtuals within itself that are overridden by descendants.
In pattern-speak we call it a "template method". The only time you
cannot do it is in the constructor.
 
Reply With Quote
 
Hunk
Guest
Posts: n/a
 
      09-11-2007
On Sep 11, 9:01 pm, Noah Roberts <(E-Mail Removed)> wrote:
> Hunk wrote:
> > On Sep 11, 7:52 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >> Hunk wrote:
> >>> I ws wondering if there is a way to implement operator+ in case of
> >>> virtual classes.
> >>> Here's the problem. I have to have a base string class from which two
> >>> classes (normal char string and a hash string class ) are derived. The
> >>> two derived classes are template classes specifying the sizes. The
> >>> base class is a non-template class so that it can be used generically
> >>> in the interface classes. the design would look like
> >>> class Base_string {
> >>> };
> >>> template<size>
> >>> class Char_string : Base_string {
> >>> };
> >>> template<size>
> >>> class Hash_string: Base_string{
> >>> };
> >>> So that in the interface class of the application he can use just the
> >>> generic Base_string to access the functions and doesnt have to know
> >>> whether its a Char or hash string
> >>> The issue is in implementing the operator+ . Since all the methods are
> >>> virtual in the base class and it should call the desired methods
> >>> polymorphically, operator+ is a challenge as it returns a Base_string
> >>> object
> >>> So if I have something like
> >>> Char_string<24> char_string1("Hello");
> >>> Char_string<24> char_string2("world");
> >>> Char_string<24> char_result;
> >>> Base_string* base_a = &char_string1;
> >>> Base_string* base_b = &char_string2;
> >>> Base_string* base_r = &char_result;
> >>> i wouldnt be able to do
> >>> *base_r = *base_a + *base_b; as the operator+ would return a
> >>> Base_object?
> >>> Any soultions to the above issue is most welcome
> >> Don't think much of overriding the operator+. Let it live in the base
> >> class, and let it return the Base_string. Overload the _assignment_
> >> operator in each of the derived classes:

>
> > If operator+ lives in the base class it would lead to errors.
> > For eg implementation for operator+ would look like

>
> > Base_string operator + (const Base_string& p_string_r) const
> > {
> > Base_string temp_str = *this;
> > temp_str.append(p_string_r.get_string()); //
> > return temp_str;
> > }
> > The problem with this is , the get_string and append are all virtual
> > in the base class... they would be overridden in the derived class...
> > for eg get_string for base class is meaningless as it does not contain
> > any data. So this would bomb out here itself. So am not sure this idea
> > would work.

>
> I can imagine that in some cases what you are saying would be true, it
> isn't a given. It is quite common to have a base class function call
> various pure virtuals within itself that are overridden by descendants.
> In pattern-speak we call it a "template method". The only time you
> cannot do it is in the constructor.- Hide quoted text -
>
> - Show quoted text -


So in effect you are saying the scenario i presented would have no
solution?

 
Reply With Quote
 
Maarten Kronenburg
Guest
Posts: n/a
 
      09-11-2007

"Hunk" wrote in message...
> Hi
>
> I ws wondering if there is a way to implement operator+ in case of
> virtual classes.
> Here's the problem. I have to have a base string class from which two
> classes (normal char string and a hash string class ) are derived. The
> two derived classes are template classes specifying the sizes. The
> base class is a non-template class so that it can be used generically
> in the interface classes. the design would look like
>


The STL string destructor is non-virtual for performance reasons (see
comment by Scott Meyers in More Effective C++), so it cannot be derived
runtime polymorphically (for virtual functions you need a virtual
destructor). So you may make such a class yourself with an STL string as a
data member. For a proper runtime polymorphic class, you may have to use the
NVI (NonVirtual Interface) design pattern, see Sutter & Alexandrescu in C++
Coding Standards. You may also look into my runtime polymorphic infinite
precision integer design, that is document N2143 available on:
http://open-std.org/jtc1/sc22/wg21/d...mailing2007-01
as it also has arithmetic operators you mention. In the Introduction, in the
section "Classes derived from class integer", you may find an answer to your
question if operator+ should return a base or derived object.
Regards, Maarten.


 
Reply With Quote
 
Hunk
Guest
Posts: n/a
 
      09-11-2007
On Sep 11, 10:35 pm, "Maarten Kronenburg" <(E-Mail Removed)>
wrote:
> "Hunk" wrote in message...
> > Hi

>
> > I ws wondering if there is a way to implement operator+ in case of
> > virtual classes.
> > Here's the problem. I have to have a base string class from which two
> > classes (normal char string and a hash string class ) are derived. The
> > two derived classes are template classes specifying the sizes. The
> > base class is a non-template class so that it can be used generically
> > in the interface classes. the design would look like

>
> The STL string destructor is non-virtual for performance reasons (see
> comment by Scott Meyers in More Effective C++), so it cannot be derived
> runtime polymorphically (for virtual functions you need a virtual
> destructor). So you may make such a class yourself with an STL string as a
> data member. For a proper runtime polymorphic class, you may have to use the
> NVI (NonVirtual Interface) design pattern, see Sutter & Alexandrescu in C++
> Coding Standards. You may also look into my runtime polymorphic infinite
> precision integer design, that is document N2143 available on:http://open-std.org/jtc1/sc22/wg21/d...mailing2007-01
> as it also has arithmetic operators you mention. In the Introduction, in the
> section "Classes derived from class integer", you may find an answer to your
> question if operator+ should return a base or derived object.
> Regards, Maarten.


I would not be using the stl string but a raw character array as data
member in the derived classes. The reason i'm developing in this way
is to better manage my memory footprint and also provide faster access
by use of hash derivation. I'll go through the section and let you
knwo if its relevant

 
Reply With Quote
 
Maarten Kronenburg
Guest
Posts: n/a
 
      09-11-2007
In addition to my previous reply I have to be a bit more precise. In my
integer class the operator+ returns a base object, which can then be
converted back to a derived object by operator=. This is to make equivalent
arithmetic expressions have equivalent results. But this can only be done if
the derived class has no extra data members (in addition to the data members
of the base class), otherwise they would be sliced off. Now when your
derived class would have extra derived data members, this slicing problem
makes things more complicated. In that case you would need also a derived
operator+ which returns a derived object. This would save your derived data
members, but then equivalent arithmetic expressions with both base and
derived objects may lead to unequivalent results. You would have to work
this out for a few used arithmetic expressions to see if that would happen.
Regards, Maarten.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments