初始化..哪一个更高效? [英] Initializing.. which one is more efficient?

查看:126
本文介绍了初始化..哪一个更高效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。

string strMyString = "SampleString";

string strMyString("SampleString");

提前感谢。

推荐答案

我回答了这里

我在这里回答一个问题:既不使用任何赋值运算符

One thing i put into this answer here: Neither is using any assignment operator.

字符串特定的东西虽然。 std :: string 有一个接受 char const * 的参数的构造函数:

Short explanation for the string specific thing though. std::string has a constructor taking one argument that accepts char const*:

// simplified to a normal class declaration. std::string actually
// is a template instantiation. 
class string {
public:
    string(char const* str) {
        // copy over...
    }
};

现在你看到有一个构造函数接受一个指向字符的指针。所以它可以接受一个字符串字面量。我认为以下情况是明显的:

Now you see that has a constructor taking a pointer to character(s). So that it can accept a string literal. I think the following case is obvious then:

string s("hello");

它将直接调用构造函数并初始化 s 。这称为直接初始化

It will call the constructor directly and initialize s thereby. This is called direct initialization.

初始化变量的另一种方法是调用复制初始化。该标准说,对于复制初始化的情况,初始化器不是它正在初始化的对象的类型,初始化器被转换为正确的类型。

The other way of initializing a variable is called copy initialization. The Standard says for the case of copy initialization where the initializer has not the type of the object it is initializing, the initializer is converted to the proper type.

// uses copy initialization
string s = "hello";

首先,我们来描述


  • s 有类型std :: string

  • hello是一个数组,在这种情况下再次像一个指针一样处理。我们将其视为 char const *

  • s has type std::string
  • "hello" is an array, which in this case again is handled like a pointer. We will therefor consider it as char const*.

编译器寻找两种方式进行转换。

The compiler looks for two ways to do the conversion.


  • 在std :: string中是否有转换构造函数?

  • 初始化器是否有一个转换操作符函数返回 std :: string

  • Is there a conversion constructor in std::string?
  • Does the initializer has a type that has a conversion operator function returning a std::string?

它将创建一个临时 std :: string 通过使用 std ::初始化对象 s string 复制构造函数。它会看到 std :: string 一个转换构造函数接受初始化。所以它使用它。最后,它实际上与

It will create a temporary std::string by one of those ways that is then used to initialize the object s by using std::string's copy constructor. And it sees std::string has a conversion constructor that accepts the initializer. So it uses it. In the end, it is effectively the same as

std::string s(std::string("hello"));

请注意,在您的示例中使用的触发所有

Note that the form that is used in your example that triggered all that

std::string s = "hello";

定义隐式转换。如果你想知道你的东西的初始化规则,你可以将 char const * 标记为显式,它不会允许使用相应的构造函数作为转换构造函数

defines an implicit conversion. You can mark the constructor taking the char const* as explicit for your types if you wonder about the initialization rules for your stuff, and it will not allow to use the corresponding constructor as a conversion constructor anymore:

class string {
public:
    explicit string(char const* str) {
        // copy over...
    }
};

这样,使用复制初始化初始化 char const * 实际上现在被禁止(在其他地方)!

With that, initializing it using a copy initialization and a char const* actually is forbidden now (and in various other places)!

如果编译器不支持在各个地方检测临时值。允许编译器假设在此上下文中复制构造函数 ,并且可以消除临时字符串的额外副本,而是直接在初始化对象中构造临时std :: string。但是,复制构造函数必须特别可访问。因此,复制初始化无效如果你这样做

Now, that was if the compiler does not support elision of temporaries at various places. The compiler is allowed to assume that a copy constructor copies in this context, and can eliminate the extra copy of the temporary string, and instead construct the temporary std::string directly into the initialized object. However, the copy constructor must be accessible in particular. So, the copy initialization is invalid if you do this

class string {
public:
    explicit string(char const* str) {
        // copy over...
    }

private: // ugg can't call it. it's private!
    string(string const&);
};

现在实际上,只有直接初始化的情况是有效的。

Now actually, only the direct initialization case is valid.

这篇关于初始化..哪一个更高效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆