施工期间向下浇筑安全吗?

发布于 2024-07-29 21:00:11 字数 390 浏览 12 评论 0原文

我有一个类层次结构,我知道给定的类 (B) 总是会派生到第二个类 (D)。 在 B 的构造函数中,如果我确定在整个构造完成之前没有人会尝试使用它,那么将 this 指针静态转换为 D* 是否安全? 就我而言,我想将对对象的引用传递给另一个类 (A)。

struct A
{
    D & d_;

    A(D & d) : d_(d) {}
};

struct D; //forward declaration

struct B
{
    A a;

    B() : a(std::static_cast<D&>(*this)) {}
};

struct D : public B
{};

这段代码安全吗?

I have a class hierarchy where I know that a given class (B) will always be derived into a second one (D). In B's constructor, is it safe to statically cast the this pointer into a D* if I'm sure that nobody will ever try to use it before the entire construction is finished? In my case, I want to pass a reference to the object to yet another class (A).

struct A
{
    D & d_;

    A(D & d) : d_(d) {}
};

struct D; //forward declaration

struct B
{
    A a;

    B() : a(std::static_cast<D&>(*this)) {}
};

struct D : public B
{};

Is this code safe?

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

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

发布评论

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

评论(3

黑寡妇 2024-08-05 21:00:12

我没有找到任何相关内容。 我很难找到你的代码不安全的原因,而这是安全的:

struct B
{
    A a;

    B(D& d) : a(d) {}
};

struct D : public B
{
    D() : B(*this) {}
};

但我可能仍然会使用我在这里提供的表格。

I didn't find anything about that. I've trouble to find reasons for which your code would be unsafe while this is safe:

struct B
{
    A a;

    B(D& d) : a(d) {}
};

struct D : public B
{
    D() : B(*this) {}
};

but I'd probably still use the form I present here.

有木有妳兜一样 2024-08-05 21:00:12

不它不是。 D 的数据成员的构造函数尚未运行。

由于 D 的成员尚未构造,因此 D 尚未完全构造,因此从技术上讲,对 D 的引用应该是无效的。 我希望这在大多数实现上都没有问题,但仍然如此。

我想提出一个更好的机制,但我想“更好”在很大程度上取决于实际细节。

No, it is not. Constructors for D's data members didn't run yet.

Since D's membrs aren't constructed, D isn't fully constructed yet, so technically, a reference to D should be invalid. I expect that to be no problem on most implementations, but still.

I'd like to suggest a better mechanism, but I guess "better" depends a lot on actual details.

好倦 2024-08-05 21:00:12

@AProgrammer的回答让我意识到static_cast通过将 this 指针从派生类传递到基类,可以轻松避免这种情况。 因此,问题归结为指向成员初始值设定项列表的 this 指针的有效性。

我在 C++ 标准 [12.6.2.7] 中发现了以下注释:

[注意:因为mem-initializer是在构造函数的范围内计算的,所以this指针可以在< em>mem-initializer 的表达式列表,用于引用正在初始化的对象。 ]

因此,在 member-initializer-list 中使用 this 是完全有效的,因此我认为所提供的代码是安全的(只要没有访问 D 的成员)。

@AProgrammer's answer made me realized that the static_cast could be easily avoided by passing the this pointer from the derived class to the base class. Consequently, the question boils down to the validity of the this pointer into the member-initializer-list.

I found the following note in the C++ Standard [12.6.2.7]:

[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. ]

Therefore, using this in the member-initializer-list is perfectly valid, so I think the code presented is safe (as long as no members of D are accessed).

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