指针和左移运算符的引用
我重新定义了<<运算符,我希望它获取指针的引用。
class foo
{
foo();
virtual ~foo();
void operator << (BaseService*& iRight);
}
在代码中的某个地方,有一个 foo 实例和一个 BaseService 类的专门化服务,我这样做:
Service* service_pointer = new Service();
foo_instance << service_pointer;
但我收到此错误: 错误:“foo_instance <<”中的“operator<<”不匹配服务指针' 注意:候选者是: void foo::operator<<(BaseService*&)
如果我将 service_pointer 动态转换为 BaseService 则不会发生任何变化 有
Service* service_pointer = new Service();
foo_instance << dynamic_cast<BaseService*>(service_pointer);
什么想法吗?
I have redefined the << operator and I want it to take a reference of pointer.
class foo
{
foo();
virtual ~foo();
void operator << (BaseService*& iRight);
}
Somewhere in the code, having a foo instance, and a service which is a specialization of the BaseService class I do :
Service* service_pointer = new Service();
foo_instance << service_pointer;
But I get this error :
error: no match for 'operator<<'in 'foo_instance << service_pointer'
note: candidates are: void foo::operator<<(BaseService*&)
Nothing changes if I dynamic_cast my service_pointer to BaseService
Service* service_pointer = new Service();
foo_instance << dynamic_cast<BaseService*>(service_pointer);
Any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个版本不起作用,因为您无法将对子类型的指针的引用传递给子类型,因此:如果
operator<<
的实现使指针指向实例MyService
(它是BaseService
的子类,但不是Service
的子类)?显然,Service*
指向MyService
是非法的。因此不允许传入Service*
。不允许使用第二个版本,因为
dynamic_cast
不返回左值,因此您不能将其作为非常量引用传递。您唯一能做的就是定义另一个
BaseService*
类型的变量,并将其作为参数传递给<<
。如果<<
然后重新分配指针,则该更改仅对新创建的变量可见,并且不会影响ServicePointer
。话虽这么说(并且不知道您的用例),我必须建议您,让
operator<<
对任何内容采用非常量引用作为其正确的操作数,这在我看来是不好的做法。您通常不会期望<<
修改它的正确操作数。The first version does not work because you can't pass in a reference to a pointer to a subtype and rightly so: What if the implementation of
operator<<
made the pointer point to an instanceMyService
(which is a subclass ofBaseService
, but not ofService
)? Clearly it would be illegal for aService*
to point to aMyService
. So passing in aService*
is not allowed.The second version is not allowed because
dynamic_cast
does not return an l-value, so you can't pass it as a non-const reference.The only thing you can do is define another variable of type
BaseService*
and pass that as an argument to<<
. If<<
then reassigns the pointer, that change will be visible for the newly created variable only and not affectServicePointer
.That being said (and not knowing your use case) I have to advice you that having
operator<<
take a non-const reference to anything as its right operand strikes me as bad practice. You wouldn't usually expect<<
to modify it's right operand.仅仅因为
Service *
类型可转换为BaseService *
类型,并不意味着Service *&
类型可转换为BaseService *&
类型。事实并非如此。这就是您的第一次调用无法编译的原因。尝试使用
dynamic_cast
(或任何其他非黑客转换)不会有帮助。这种转换的结果不是左值。并且您不能将非常量引用绑定到非左值的内容。这就是您的第二次调用无法编译的原因。如果您确实希望运营商专门接受
BaseService *&
,那么您只能将Service *
手动预转换为左值 类型为BaseService *
(即显式指针对象),然后使用该左值调用运算符如果您更改了对运算符
<<
的调用,则会编译声明 if 操作符 to但你是否可以这样做取决于根据你的意图。为什么要尝试通过引用传递指针?
Just because
Service *
type is convertible toBaseService *
type does not in any way mean thatService *&
type is convertible toBaseService *&
type. It isn't. This is why your first call does not compile.Trying to use
dynamic_cast
(or any other non-hacking cast) will not help. The result of such cast is not an lvalue. And you cannot bind a non-const reference to something that is not an lvalue. This is why your second call does not compile.If you really want your operator to accept specifically
BaseService *&
, then you are limited to performing a manual pre-conversion ofService *
to an lvalue of typeBaseService *
(i.e. to an explicit pointer object) and then calling the operator with that lvalueYour calls to operator
<<
will compile if you change the declaration if the operator tobut whether you can do it or not depends on your intent. Why are you trying to pass a pointer by reference?
您必须实际定义一个
BaseService
指针变量。由于您想要引用指针,因此它不能是右值。
另请注意,这里不需要
dynamic_cast
。static_cast<>
就可以解决这个问题。更好的是,使用从service_pointer
到base_service_pointer
的简单分配!You have to actually define a
BaseService
pointer variable.As you want to take a reference to the pointer, it cannot be an rvalue.
Also note that the
dynamic_cast<>
it not necessary here. Astatic_cast<>
would to the trick. Even better, use a simple assignment fromservice_pointer
tobase_service_pointer
!这不可能工作,因为引用是非常量,因此您可以更改 foo::operator<< 中的指针值。考虑以下示例:
This can't possibly work because reference is non-const, so you can change the pointer value in foo::operator<<. Consider the following example: