指针和左移运算符的引用

发布于 2024-12-02 07:46:41 字数 677 浏览 1 评论 0原文

我重新定义了<<运算符,我希望它获取指针的引用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深已缘浅 2024-12-09 07:46:41

第一个版本不起作用,因为您无法将对子类型的指针的引用传递给子类型,因此:如果 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 instance MyService (which is a subclass of BaseService, but not of Service)? Clearly it would be illegal for a Service* to point to a MyService. So passing in a Service* 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 affect ServicePointer.

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.

同展鸳鸯锦 2024-12-09 07:46:41

仅仅因为 Service * 类型可转换为 BaseService * 类型,并不意味着 Service *& 类型可转换为 BaseService *& 类型。事实并非如此。这就是您的第一次调用无法编译的原因。

尝试使用dynamic_cast(或任何其他非黑客转换)不会有帮助。这种转换的结果不是左值。并且您不能将非常量引用绑定到非左值的内容。这就是您的第二次调用无法编译的原因。

如果您确实希望运营商专门接受 BaseService *&,那么您只能将 Service * 手动预转换为左值 类型为 BaseService *(即显式指针对象),然后使用该左值调用运算符

Service* service_pointer = new Service();
BaseService* base_service_pointer = service_pointer;
foo_instance << base_service_pointer;

如果您更改了对运算符 << 的调用,则会编译声明 if 操作符 to

void operator <<(BaseService* const& iRight);

但你是否可以这样做取决于根据你的意图。为什么要尝试通过引用传递指针?

Just because Service * type is convertible to BaseService * type does not in any way mean that Service *& type is convertible to BaseService *& 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 of Service * to an lvalue of type BaseService * (i.e. to an explicit pointer object) and then calling the operator with that lvalue

Service* service_pointer = new Service();
BaseService* base_service_pointer = service_pointer;
foo_instance << base_service_pointer;

Your calls to operator << will compile if you change the declaration if the operator to

void operator <<(BaseService* const& iRight);

but whether you can do it or not depends on your intent. Why are you trying to pass a pointer by reference?

ゃ人海孤独症 2024-12-09 07:46:41

您必须实际定义一个BaseService 指针变量。

Service* service_pointer = new Service();
BaseService* base_service_pointer = dynamic_cast<BaseService*>(service_pointer);
foo_instance << base_service_pointer;

由于您想要引用指针,因此它不能是右值。

另请注意,这里不需要 dynamic_caststatic_cast<> 就可以解决这个问题。更好的是,使用从 service_pointerbase_service_pointer 的简单分配!

You have to actually define a BaseService pointer variable.

Service* service_pointer = new Service();
BaseService* base_service_pointer = dynamic_cast<BaseService*>(service_pointer);
foo_instance << base_service_pointer;

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. A static_cast<> would to the trick. Even better, use a simple assignment from service_pointer to base_service_pointer!

¢蛋碎的人ぎ生 2024-12-09 07:46:41

这不可能工作,因为引用是非常量,因此您可以更改 foo::operator<< 中的指针值。考虑以下示例:


class Service1 : BaseService
{
};

class Service2 : BaseService
{
};

void foo::operator << (BaseService*& iRight)
{
    // ok, iRight is a reference to BaseService*
    // so we can assign Service2* to it, can't we?
    iRight = new Service2();
}

Service1* service_pointer = new Service1();
foo_instance << service_pointer; // oops...

This can't possibly work because reference is non-const, so you can change the pointer value in foo::operator<<. Consider the following example:


class Service1 : BaseService
{
};

class Service2 : BaseService
{
};

void foo::operator << (BaseService*& iRight)
{
    // ok, iRight is a reference to BaseService*
    // so we can assign Service2* to it, can't we?
    iRight = new Service2();
}

Service1* service_pointer = new Service1();
foo_instance << service_pointer; // oops...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文