类实例化语法

发布于 2024-12-12 06:15:51 字数 1332 浏览 0 评论 0原文

我一直被教导说

1. Class c(arg);

2. Class c = arg;

是两个完全等价的陈述,但看看这种情况。

#include <iostream>

class Intermediary {
};

class Left {
  public:
    Left(const Intermediary &) {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }   
};

class Right {
  public:
    // The argument is there just so that the example can work, see below
    Right(int) { 
        std::cout << __PRETTY_FUNCTION__  << std::endl;
    }

    operator Intermediary () const {
        std::cout << __PRETTY_FUNCTION__  << std::endl;

        return Intermediary();    
    }
};

现在如果我这样做:

Left l = Right(0);

编译器会抱怨

error: conversion from Right to non-scalar type Left requested

但是如果我这样做:

Left l(Right(0)); 

那么一切都会编译并且输出是

Right::Right(int)
Right::operator Intermediary() const
Left::Left(const Intermediary&)

但是,如果我这样做:

Left l = (Intermediary)Right(0);

那么一切都会再次编译并且输出就像上面的一样。

那么显然

1. Class c(arg);

2. Class c = arg;

不一样,但是为什么不一样,又有什么区别呢?我在网上找不到任何关于此的信息。

I've always been taught that

1. Class c(arg);

and

2. Class c = arg;

are two totally equivalent statements, but look at this situation.

#include <iostream>

class Intermediary {
};

class Left {
  public:
    Left(const Intermediary &) {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }   
};

class Right {
  public:
    // The argument is there just so that the example can work, see below
    Right(int) { 
        std::cout << __PRETTY_FUNCTION__  << std::endl;
    }

    operator Intermediary () const {
        std::cout << __PRETTY_FUNCTION__  << std::endl;

        return Intermediary();    
    }
};

Now if I do this:

Left l = Right(0);

The compiler will complain

error: conversion from Right to non-scalar type Left requested

But if I do this:

Left l(Right(0)); 

Then everything compiles and the output is

Right::Right(int)
Right::operator Intermediary() const
Left::Left(const Intermediary&)

However, if I do this:

Left l = (Intermediary)Right(0);

then everything compiles again and the output is just like the one above.

So obviously

1. Class c(arg);

and

2. Class c = arg;

are not the same, but why not, and what's the difference? I couldn't find anything about this online.

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

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

发布评论

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

评论(1

本宫微胖 2024-12-19 06:15:51

我一直被告知 Class c(arg);Class c = arg; 是两个完全等效的语句,但看看这种情况。 p>

事实证明它们并不等同。第一个从 arg 构造出 Class c,而第二个则从 arg 构造出 Class > 然后复制构造 Class c 。请注意,实现可以省略该副本,而且通常会这样做。

Left l = Right(0);

这需要从 RightIntermediary 的转换,以及从 IntermediaryLeft 的转换。标准不允许这两个连续的用户定义转换,您必须至少明确其中之一,如下所示:

Left l = (Intermediary)Right(0);

I've always been taught that Class c(arg); and Class c = arg; are two totally equivalent statements, but look at this situation.

It turns out they are not equivalent. The first one constructs the Class c out of an arg, while the second one constructs a Class out of an arg and then copy-constructs Class c out of it. Note that the implementation is allowed to ellide that copy, and it usually does.

Left l = Right(0);

This requires a conversion from Right to Intermediary, and one from Intermediary to Left. This two sequential user defined conversions are not allowed by the standard, you have to do at least one of them explicit as you do with:

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