471,337 Members | 1,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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

Sep 11 '07 #1
14 1998
On 11 , 18:17, Hunk <santosh.udyav...@gmail.comwrote:
Hi
I ws wondering if there is a way to implement operator+ in case of
virtual classes.
Char_string<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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.

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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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
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...@comAcast.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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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 -

Sep 11 '07 #5
Hunk wrote:
On Sep 11, 7:52 pm, "Victor Bazarov" <v.Abaza...@comAcast.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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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.
Sep 11 '07 #6
On Sep 11, 9:01 pm, Noah Roberts <u...@example.netwrote:
Hunk wrote:
On Sep 11, 7:52 pm, "Victor Bazarov" <v.Abaza...@comAcast.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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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?

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...@inter.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:Base_string
{ char d_the_string[size];
size_t d_size;
};
template<size>
Class Hash_string:Base_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:Base_string
{ char d_the_string[size];
size_t d_size;
};
template<size>
Class Hash_string:Base_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
On Sep 12, 12:33 am, "Maarten Kronenburg" <M.Kronenb...@inter.nl.net>
wrote:
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:Base_string
{ char d_the_string[size];
size_t d_size;
};
template<size>
Class Hash_string:Base_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.- Hide quoted text -

- Show quoted text -
Marteen,
The design you provide necessates that hash be performed outside the
class and be served as a parameter in the constructor. But in my
design, the hash string class itself should provide for computation of
hash. In essence by choosing a hash_string over a char_string , the
user is implying that i have a lot oc comparisons to do hence compare
the strings using the hash value. Usabilitry wise it would behave
identically to char_string. I had a desing using policy classes that
waorked , but the change in design necessated from the fact that a
common interface a.k.a a non-template base_string generic class would
be needed . Now i'm stuck with this overriding of virtual class wich
returns by value paradox. I'll go thrrough the NVI design pattern
hopefully to pick up some clue

Sep 12 '07 #11
On Sep 11, 4:17 pm, Hunk <santosh.udyav...@gmail.comwrote:
I ws wondering if there is a way to implement operator+ in case of
virtual classes.
In general, polymorphism and operator overloading don't work
well together; operator overloading requires a value type for
the return values. (In cases where it is necessary, the
letter/envelop idiom can be used to may a class with value
semantics act polymorphically.)
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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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?
Just a general question: if I add a Char_string< 16 and a
Hash_string< 14 >, what concrete type should the results have?
This is an important aspect of the problem. But not the only
aspect. Typically, for addition if there are N different
derived types, then you have NxN different functions to
implement, each with potentially a different return type (or
different rules for determining the return type).

I'm not sure what the difference is between a Hash_string and a
Char_string, but for just a single hierarchy, where the derived
types only differ in length, you can probably work something out
with templates, e.g.:

template< size_t L, size_t R >
FixedLengthString< L + R >
operator+( FixedLengthString< L const& lhs,
FixedLengthString< R const& rhs ) ;

Trying to use polymorphism here, however, will not work.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 12 '07 #12
On Sep 12, 4:26 pm, James Kanze <james.ka...@gmail.comwrote:
On Sep 11, 4:17 pm, Hunk <santosh.udyav...@gmail.comwrote:
I ws wondering if there is a way to implement operator+ in case of
virtual classes.

In general, polymorphism and operator overloading don't work
well together; operator overloading requires a value type for
the return values. (In cases where it is necessary, the
letter/envelop idiom can be used to may a class with value
semantics act polymorphically.)
You are right... learnt it the hard way... you say there is some way
in letter/envelop idiom?
For now i have taken the decision to restrict the user to use only +=.
I know this is a very restrictive
way , but for now i know of no other way to tackle the issue
>

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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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?

Just a general question: if I add a Char_string< 16 and a
Hash_string< 14 >, what concrete type should the results have?
This is an important aspect of the problem. But not the only
aspect. Typically, for addition if there are N different
derived types, then you have NxN different functions to
implement, each with potentially a different return type (or
different rules for determining the return type).
Thats a good question. I would prefer giving a compilation error for
this.
This is for the simple reason that Hash_string data members are
different than
Char_string. So would'nt know which to return
I'm not sure what the difference is between a Hash_string and a
Char_string, but for just a single hierarchy, where the derived
types only differ in length, you can probably work something out
with templates, e.g.:
The difference in Hash_string and Char_string is that the Hash_string
class has a
hash value computed and stored as a data member. So comparisons are
faster as they
would be comparing integers rather than characters.
The generic base_string is required as for the interface class a
generic version is required.
>
template< size_t L, size_t R >
FixedLengthString< L + R >
operator+( FixedLengthString< L const& lhs,
FixedLengthString< R const& rhs ) ;

Trying to use polymorphism here, however, will not work.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -

- Show quoted text -

Sep 12 '07 #13
Marteen,
The design you provide necessates that hash be performed outside the
class and be served as a parameter in the constructor. But in my
design, the hash string class itself should provide for computation of
hash. In essence by choosing a hash_string over a char_string , the
user is implying that i have a lot oc comparisons to do hence compare
the strings using the hash value. Usabilitry wise it would behave
identically to char_string. I had a desing using policy classes that
waorked , but the change in design necessated from the fact that a
common interface a.k.a a non-template base_string generic class would
be needed . Now i'm stuck with this overriding of virtual class wich
returns by value paradox. I'll go thrrough the NVI design pattern
hopefully to pick up some clue
If I understand correctly, the hash value is a function of the string
itself. In that case for the derived overridden member function add (with
argument ref base class) there would be no problem, because adding the
string with hash would mean first adding the strings themselves, and after
that recomputing the hash value, no matter if the argument was base or
derived. For the binary operator+ returning a base class object is also no
problem, because after that the operator= is called which calls the derived
overridden member function assign, where the hash is recomputed after the
strings are assigned. In other words, when the hash value is just a function
of the string values, it doesn't matter if they are sliced off, because they
can be recomputed. Unless you want to recompute the hash values from the
argument hash values, and not from the resulting string.
Regards, Maarten.
Sep 12 '07 #14
Hunk wrote:
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<24char_string1("Hello");
Char_string<24char_string2("world");
Char_string<24char_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 it's just two derived classes, you might get away with something like
this:
#include <iostream>
#include <tr1/memory>
#include <cassert>
class Base {
public:

Base ( void ) {}

virtual
void some_method ( void ) const = 0;

};

class DerivedA : public Base {
public:

DerivedA ( void )
: Base()
{}

virtual
void some_method ( void ) const {
std::cout << "DerivedA\n";
}

};

class DerivedB : public Base {
public:

DerivedB ( void )
: Base ()
{}

virtual
void some_method ( void ) const {
std::cout << "DerivedB\n";
}

};
class Sum {

enum type { derived_a, derived_b };

typedef std::tr1::shared_ptr<Basepointer;
pointer the_ptr;
type the_type;

public:

Sum ( Base const & lhs, Base const & rhs )
: the_ptr ()
{
if ( ( dynamic_cast<DerivedA const *>( &lhs ) != 0 )
&&
( dynamic_cast<DerivedA const *>( &rhs ) != 0 ) ) {
the_ptr = pointer( new DerivedA () );
the_type = derived_a;
}
if ( ( dynamic_cast<DerivedB const *>( &lhs ) != 0 )
&&
( dynamic_cast<DerivedB const *>( &rhs ) != 0 ) ) {
the_ptr = pointer( new DerivedB () );
the_type = derived_b;
}
assert( the_ptr != 0 );
}

operator Base & ( void ) {
return ( *the_ptr );
}

operator Base const & ( void ) const {
return ( *the_ptr );
}

operator DerivedA & ( void ) {
assert( the_type == derived_a );
return ( static_cast<DerivedA &>( *the_ptr ) );
}

operator DerivedA const & ( void ) const {
assert( the_type == derived_a );
return ( static_cast<DerivedA const &>( *the_ptr ) );
}

operator DerivedB & ( void ) {
assert( the_type == derived_b );
return ( static_cast<DerivedB &>( *the_ptr ) );
}

operator DerivedB const & ( void ) const {
assert( the_type == derived_b );
return ( static_cast<DerivedB const &>( *the_ptr ) );
}
void some_method ( void ) const {
the_ptr->some_method();
}

};

Sum operator+ ( Base const & lhs, Base const & rhs ) {
return ( Sum( lhs, rhs ) );
}

void test ( Base & b ) {
b.some_method();
}

int main ( void ) {
DerivedA a1;
DerivedA a2;
( a1 + a2 ).some_method();
DerivedA a;
a = a1 + a2;
DerivedB b1;
DerivedB b2;
test( b1 + b2 );
DerivedB b ( b1 + b2 );
}

The user-defined converions can lead to ambiguities, and there are probably
some other problems.

Question: what is the purpose of a common base class for these two string
types? Are users really supposed to use Base_string* polymorphically? If
you don't really need runtime polymorphism, the string classes could be
independent and common functionality can be supplied by templates and
overloaded functions.
Best

Kai-Uwe Bux
Sep 13 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by CoolPint | last post: by
9 posts views Thread by richard.forrest1 | last post: by
12 posts views Thread by c++novice | last post: by
15 posts views Thread by Heiner | last post: by
8 posts views Thread by Floogle | last post: by
6 posts views Thread by Rob McDonald | last post: by
reply views Thread by rosydwin | last post: by

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.