创建自定义 std 流实现时出现编译器警告

发布于 2024-11-27 21:57:39 字数 767 浏览 2 评论 0原文

我有一个 Visual Studio 2008 C++ 应用程序,在其中创建了自定义 Streambuf 和流实现。我使用了 Angelika Langer 给出的方法来创建下面详细介绍了流:

class MyOutputStream_3 : private MyStreamBuf, public ostream {
public:
    MyOutputStream_3() : ostream((MyStreamBuf*) this), ios(0) {};
    virtual ~MyOutputStream_3() { sync(); }
    // ...
}; 

但是,Visual Studio 给了我一个 警告

warning C4355: 'this' : used in base member initializer list

代码工作正常,但我我担心编译器会通知我,我所做的事情可能会在某些情况下崩溃或者可能是不可移植的。

在这种情况下我可以忽略这一点吗?或者我应该采取什么措施来解决我的问题?

I have a Visual Studio 2008 C++ application where I've created a custom streambuf and stream implementation. I've used an approach given by Angelika Langer for creating the stream detailed below:

class MyOutputStream_3 : private MyStreamBuf, public ostream {
public:
    MyOutputStream_3() : ostream((MyStreamBuf*) this), ios(0) {};
    virtual ~MyOutputStream_3() { sync(); }
    // ...
}; 

But, Visual Studio gives me a warning:

warning C4355: 'this' : used in base member initializer list

The code works fine, but I'm worried the compiler is informing me that what I've done either could break under some circumstances or may be non-portable.

Is this something I can ignore in this instance or what should I do to fix my issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

七分※倦醒 2024-12-04 21:57:39

它警告您有关在初始化列表中使用 this 的信息,因为从技术上讲,this 尚不存在,因为构造函数尚未完成执行(也没有任何成员对象)并将其传递给函数可能会在尝试使用该对象(其中包含尚未调用其构造函数的对象)时导致问题。

如果您只是存储指针以供以后使用,那么该代码应该可以工作。但是,当您回到这段代码并忘记您不应该使用该指针时,您可能会犯一个错误,并且无法解释程序崩溃的原因。

以下是该警告的 Microsoft 页面: http://msdn.microsoft.com/ en-us/library/3c594ae3.aspx

It's warning you about the use of this in the initializer list because technically, this doesn't exist yet because the constructor hasn't finished executing yet (nor have the constructors of any member objects) and passing it to a function could cause problems when they try to use that object (which contains objects whose constructors haven't been called).

The code should work if you're just storing the pointer for use later. But when you come back to this code and forget that you're not supposed to use that pointer, you might make a mistake and not be able to explain why your program is crashing.

Here is the Microsoft page for that warning: http://msdn.microsoft.com/en-us/library/3c594ae3.aspx

浊酒尽余欢 2024-12-04 21:57:39

C++03 标准在 12.6.2/7“初始化基数和成员”的注释中有这一点:

[注意:因为 mem-initializer 是在构造函数的范围内求值的,所以可以在 mem-initializer 的表达式列表中使用 this 指针来引用正在初始化的对象。 ]

我认为发出警告是因为 this 指针引用的对象未完全初始化,因此存在一些潜在的危险。只要您的基础在初始化之前不实际使用对象的未初始化部分,就应该没问题。

作为警告的 MSDN 文档 (http://msdn.microsoft.com/en -us/library/3c594ae3.aspx)提及:

实际上,您已将指向未构造对象的指针传递给另一个构造函数。如果其他构造函数访问任何成员或调用此构造函数的成员函数,结果将是未定义的。

The C++03 standard has this bit in a note for 12.6.2/7 "Initializaing bases and members":

[Note: because the mem-initializer are evaluated in the scope of the constructor, the this pointer can be used in the expression-list of a mem-initializer to refer to the object being initialized. ]

I think the warning is issued becuase the object being referred to by the this pointer isn't fully initialized so there's some potential danger. As long as your base doesn't actually use the uninitialized parts of the object until after they're initialized, you should be fine.

As the MSDN docs for the warning (http://msdn.microsoft.com/en-us/library/3c594ae3.aspx) mention:

In effect, you've passed a pointer to an unconstructed object to another constructor. If those other constructors access any members or call member functions on this, the result will be undefined.

骄傲 2024-12-04 21:57:39

顺便说一句,只有当您在 mem-initializer 列表中显式使用 this 时才会发生这种情况。传递尚未构造的成员变量的地址不会引发 C4355。

如果您需要证明,请查看 fstream 的构造函数。

Incidentally, this only happens when you explicitly use this in the mem-initializer list. Passing the address of a member variable that has not yet been constructed does not raise C4355.

Take a look at fstream's constructor if you need proof.

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