473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual operator +

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<24c har_string1("He llo");
Char_string<24c har_string2("wo rld");
Char_string<24c har_result;
Base_string* base_a = &char_string 1;
Base_string* base_b = &char_string 2;
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

Sep 11 '07 #1
14 2329
On 11 , 18:17, Hunk <santosh.udyav. ..@gmail.comwro te:
Hi
I ws wondering if there is a way to implement operator+ in case of
virtual classes.
Char_string<24c har_string1("He llo");
Char_string<24c har_string2("wo rld");
Char_string<24c har_result;
Base_string* base_a = &char_string 1;
Base_string* base_b = &char_string 2;
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<De rived>(result_o f_op_plus) and assign. But this is you
work of'course, compiler shouldn't allow you convert Base to Derived
by default, automatically.

Sep 11 '07 #2
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<24c har_string1("He llo");
Char_string<24c har_string2("wo rld");
Char_string<24c har_result;
Base_string* base_a = &char_string 1;
Base_string* base_b = &char_string 2;
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
Sep 11 '07 #3
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...

Sep 11 '07 #4
On Sep 11, 7:52 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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<24c har_string1("He llo");
Char_string<24c har_string2("wo rld");
Char_string<24c har_result;
Base_string* base_a = &char_string 1;
Base_string* base_b = &char_string 2;
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 -

Sep 11 '07 #5
Hunk wrote:
On Sep 11, 7:52 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>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
polymorphical ly, operator+ is a challenge as it returns a Base_string
object
So if I have something like
Char_string<2 4char_string1(" Hello");
Char_string<2 4char_string2(" world");
Char_string<2 4char_result;
Base_string * base_a = &char_string 1;
Base_string * base_b = &char_string 2;
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.
Sep 11 '07 #6
On Sep 11, 9:01 pm, Noah Roberts <u...@example.n etwrote:
Hunk wrote:
On Sep 11, 7:52 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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<siz e>
class Char_string : Base_string {
};
template<siz e>
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
polymorphicall y, operator+ is a challenge as it returns a Base_string
object
So if I have something like
Char_string<24 char_string1("H ello");
Char_string<24 char_string2("w orld");
Char_string<24 char_result;
Base_string* base_a = &char_string 1;
Base_string* base_b = &char_string 2;
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?

Sep 11 '07 #7
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.
Sep 11 '07 #8
On Sep 11, 11:09 pm, "Maarten Kronenburg" <M.Kronenb...@i nter.nl.net>
wrote:
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.
Bull's eye... my base class has no data members and derived class have
all the members
the design is like
class Base_string{};

template<size>
Class Char_string:Bas e_string
{ char d_the_string[size];
size_t d_size;
};
template<size>
Class Hash_string:Bas e_string
{ char d_the_string[size];
size_t d_size;
int d_hash; };

so here i'm stuck while overriding the virtual operator+ in the
Base_string as it would mess up what it is pointing to (eg in the
earlier post)

Sep 11 '07 #9
Bull's eye... my base class has no data members and derived class have
all the members
the design is like
class Base_string{};

template<size>
Class Char_string:Bas e_string
{ char d_the_string[size];
size_t d_size;
};
template<size>
Class Hash_string:Bas e_string
{ char d_the_string[size];
size_t d_size;
int d_hash; };

so here i'm stuck while overriding the virtual operator+ in the
Base_string as it would mess up what it is pointing to (eg in the
earlier post)

Personally I understand that the hashed string has different addition
behaviour than a normal string. But personally for reasons of simplicity I
would make a single class hash_string which always has a data member int
d_hash, but when d_hash is zero, the class behaves like a normal string, and
I would make the size not a template parameter, but a constructor parameter,
but also use a char type template parameter.
Just as another line of thought:
template<class charT>
class hash_string
{ charT * the_string;
size_t the_size;
unsigned int the_hash;
public:
hash_string( size_t asize, unsigned int ahash = 0 )
: the_size( asize ), the_hash( ahash )
{ the_string = new charT [ asize ];
};
~hash_string()
{ delete [] the_string;
};
Now the member functions and operators like operator+= do a hash only if
the_hash is not zero. You would have to decide what the binary operator+
must do when only one of the arguments the_hash is zero, or when they are
different. This way you don't need derivation at all, which in my opinion in
this case saves you some problems.
Regards, Maarten.

Sep 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2174
by: CoolPint | last post by:
I read that the return type has to be exactly same for a virtual function to be overriden. While testing something, I discovered something I cannot understand. I cannot understand why the code below compiles. I see that it won't compile if I have "virtual char & vtest() { }" in Base class and have "virtual const char & vtest() { }" in Derived class. But it compiles if I have "virtual const char & vtest() { }" in Base
9
5013
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the same abstract Interface class. Interface has a pair of virtual functions begin() and end() that generate a typical STL style range. Classes A and B provide different implementations, maybe using different types of container. The problem is that, to...
12
1782
by: c++novice | last post by:
1--Can operators be virtual? 2--What is the difference between an operator= returning a refernce Vs a value?
17
3730
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how would I proceed with operator=? Do I need to supply 2 operators:
15
1882
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
8
17747
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
3
3931
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. template<class Type> class base { public: base& operator/=(const base&); Type *image;
11
3420
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
17
3522
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
6
4725
by: Rob McDonald | last post by:
I would like to force all the classes in my hierarchy to implement the << operator for testing purposes. My base class is a pure virtual class. I started out by working with operator overloading in the derived class. I have been trying to use what I learned there to create an appropriate virtual class to force overloading. class Base{
0
7934
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.