虚拟运算符+ [英] virtual operator +
问题描述
嗨
我想知道是否有办法在
虚拟课程中实施操作员。
这里这是问题所在。我必须有一个基本字符串类,从中派生出两个
类(普通字符串和散列字符串类)。
两个派生类是指定大小的模板类。
基类是一个非模板类,因此它可以在接口类中一般使用
。设计看起来像是
类Base_string {
};
模板< size>
class Char_string:Base_string {
};
template< size>
class Hash_string:Base_string {
};
因此,在应用程序的接口类中,他只能使用
泛型Base_string来访问函数,而不必知道
是否为Char或哈希字符串
问题在于实现operator +。由于基类中的所有方法都是虚拟的,并且它应该以多态方式调用所需的方法
,因此运算符+是一个挑战,因为它返回一个Base_string
object
所以如果我有类似的东西
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无法做到
* base_r = * base_a + * base_b;作为操作员+会返回一个
Base_object?
我们非常欢迎任何上述问题的灵魂来源
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
推荐答案
11,18:17,Hunk< santosh.udyav ... @ gmail.comwrote:
On 11 , 18:17, Hunk <santosh.udyav...@gmail.comwrote:
Hi
Hi
我想知道是否有办法在
虚拟类的情况下实现operator +。
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 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;
我无法做到
* base_r = * base_a + * base_b;作为运算符+会返回一个
Base_object?
对上述问题的任何灵魂都是最受欢迎的
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
如果你在上下文中调用operator +,编译器不知道真正的lhs&和
(动态,运行时)类型。 op +的rhs对象,它当然是
会期望op +的返回类型是在operator +中指定的
使用静态类型的lhs,rhs。
示例:for(Base *,Base *),如果op +返回Base,编译器期望
Base,并拒绝您像往常一样将Base分配给Derived。
如果你确定你的Base-s在这个上下文中总是指针/
引用真实(动态,运行时)Derived对象,你可以
dynamic_cast< Derived> (result_of_op_plus)并分配。但是这就是你的工作,编译器不应该允许你自动将Base转换为Derived
。
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.
Hunk写道:
Hunk wrote:
我想知道是否有办法在
$ b的情况下实现operator + $ b虚拟课程。
这就是问题所在。我必须有一个基本字符串类,从中派生出两个
类(普通字符串和散列字符串类)。
两个派生类是指定大小的模板类。
基类是一个非模板类,因此它可以在接口类中一般使用
。设计看起来像是
类Base_string {
};
模板< size>
class Char_string:Base_string {
};
template< size>
class Hash_string:Base_string {
};
因此,在应用程序的接口类中,他只能使用
泛型Base_string来访问函数,而不必知道
是否为Char或哈希字符串
问题在于实现operator +。由于基类中的所有方法都是虚拟的,并且它应该以多态方式调用所需的方法
,因此运算符+是一个挑战,因为它返回一个Base_string
object
所以如果我有类似的东西
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无法做到
* base_r = * base_a + * base_b;作为运算符+将返回一个
Base_object?
对上述问题的任何灵魂都是最受欢迎的
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不要过多地考虑操作员+。让它住在基础
类中,让它返回Base_string。在每个派生类中重载_assignment_
运算符:
template< size>
class Char_string:Base_string {
Char_string& operator =(Base_string const&){
//做什么需要
返回* this;
}
};
模板< size>
类Hash_string:Base_string {
Hash_string& operator =(Base_string const&){
//做什么需要
返回* this;
}
};
这样你可以将operator +的结果赋给正确的
对象。并且将调用正确的operator =函数。在里面做什么
你需要什么。
V
-
请删除资金''A'在通过电子邮件回复时
我没有回复最热门的回复,请不要问
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
另请注意,op + for(Base&,Base&)应该真正返回Derived类型 -
所以,如果你想要不仅有一个
派生算子+(Base& amp; amp; amp; ;,Base&)
总是为任何给定的Base * s返回Derived,但是
Base * operator +(Base&,Base&)的家族
返回Derived仅适用于Derived args,op应该返回结果
而不是值......
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...
这篇关于虚拟运算符+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!