This question already has an answer here:
这个问题在这里已有答案:
- Fluent interfaces and inheritance in C++ 5 answers
C ++ 5答案中的流畅接口和继承
Let's say I have a class `A' with lot's of methods (which I can't grasp eventually) including a lot of fluent interfaces (including operator overloads) – returning *this
by reference – which could become more in a later API version or so on.
假设我有一个带有很多方法的类'A'(我最终无法掌握),包括许多流畅的接口(包括运算符重载) - 通过引用返回*这可能会在以后的API版本中变得更多或者等等。
I'd like to inherit all that stuff in a class `B' which has additional (fluent) methods so that all fluent interfaces of `A' (I don't want really to care about) return *this
references as they were of type `B'.
我想在类'B'中继承所有那些东西,它有额外的(流畅的)方法,所以所有流畅的接口'A'(我真的不想真正关心)返回*这个引用,因为它们是输入“B”。
(How) Can I do that?
(我怎样才能做到这一点?
1 个解决方案
#1
0
After its been casted as its base type you get object slicing.
在将其作为基本类型进行转换后,您将获得对象切片。
Say for object B b
the this
value is of type B*
. When using its A
member functions, this
will be of the type A*
. If you cast the A*
as an A
object you will lose reference to all of its B
specific data during the partial assignment.
对于对象B b,该值是B *类型。使用其A成员函数时,它将是A *类型。如果将A *强制转换为A对象,则在部分赋值期间将丢失对其所有B特定数据的引用。
#1
0
After its been casted as its base type you get object slicing.
在将其作为基本类型进行转换后,您将获得对象切片。
Say for object B b
the this
value is of type B*
. When using its A
member functions, this
will be of the type A*
. If you cast the A*
as an A
object you will lose reference to all of its B
specific data during the partial assignment.
对于对象B b,该值是B *类型。使用其A成员函数时,它将是A *类型。如果将A *强制转换为A对象,则在部分赋值期间将丢失对其所有B特定数据的引用。