简体   繁体   English

您使用什么方法初始化成员变量?

[英]What method do you use to initialize member variables?

Suppose I have a class Foo , with a member variable that is a std::vector of floats, bar . 假设我有一个Foo类,其成员变量是floats, barstd::vector I then create an instance of Foo , called foo . 然后,我创建一个名为fooFoo实例。 Let's say that I do not know the length of bar before the program runs, but at the point when foo 's constructor is called, I know that it's length should be x . 假设我在程序运行之前不知道bar的长度,但是在调用foo的构造函数时,我知道它的长度应该为x

There are three ways I can think of to initialize the length of bar , and I'm just wondering which of the three most people tend to use. 我可以考虑三种方法来初始化bar的长度,而我只是想知道大多数人倾向于使用三种方法中的哪一种。 I have ranked them in order of what I would consider to be "best practice", but I'm more curious about what method people "actually" use in practice. 我已经按照我认为是“最佳实践”的顺序对其进行了排名,但是我对人们在实践中“实际”使用哪种方法感到更加好奇。 Sometimes I use methods which make the code clearer to follow, rather than necessarily following best practice... 有时我使用一些使代码更易于遵循的方法,而不必遵循最佳实践。

  1. bar is a private member, and I resize it during foo's constructor, eg Foo foo(x){bar.resize(x)}; bar是私有成员,我在foo's构造函数中调整其大小,例如Foo foo(x){bar.resize(x)};

  2. bar is a private member, and I call foo.ResizeBar(x) which resizes bar internally, after creating foo . bar是私有成员,在创建foo之后,我调用foo.ResizeBar(x)在内部调整bar大小。

  3. bar is a public member, and I call foo.bar.resize(x) , after creating foo . bar是公共成员,在创建foo之后,我调用foo.bar.resize(x)

Or, in code: 或者,在代码中:

1. 1。

class Foo
{
private:
    std::vector<float> bar;
public:
    Foo(int x)
    {
        bar.resize(x);
    };
};

int main()
{
    Foo foo(100);
    return 0;
}

2. 2。

class Foo
{
private:
    std::vector<float> bar;
public:
    Foo()
    {
    };
    void ResizeBar(int x)
    {
        bar.resize(x);
    };
};

int main()
{
    Foo foo;
    foo.ResizeBar(100);
    return 0;
}

3. 3。

class Foo
{
public:
    std::vector<float> bar;
    Foo()
    {
    };
};

int main()
{
    Foo foo;
    foo.bar.resize(100);
    return 0;
}

The problem with all three of your methods is that you're needlessly invoking the default initializer for your vector, and then modifying the already initialized vector to suit your needs. 这三种方法的问题在于,您不必要地为向量调用默认的初始化程序, 然后修改已初始化的向量以满足您的需要。

You should be invoking the correct initializer using the initializer list: 您应该使用初始化器列表调用正确的初始化器:

Foo::Foo(std::size_t x) : bar(x, 0) { }

The best method is not in the list of options you posted. 最好的方法不在您发布的选项列表中。 The best method is to initialize bar using member initializer lists . 最好的方法是使用成员初始化程序列表初始化bar

Foo::Foo(int x) : bar(x) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++类中,是否必须初始化数组的成员变量? - In a C++ class, do you have to initialize member variables that are arrays? 即使您没有将结构显式定义为指针,为什么还要使用 -&gt; 来访问结构成员变量? - Why do you use -> to access struct member variables even if you haven't explicitly defined the struct as a pointer? 如何初始化成员变量 - How to Initialize Member Variables 初始化const成员变量 - Initialize const member variables 我在构造函数中使用什么类型来初始化由无名枚举定义的成员变量? - What type do I use in the constructor to initialize a member variable defined by a nameless enum? 在多个对象中初始化预定义成员变量的正确方法是什么? - What is the proper way to initialize predefined member variables in multiple objects? 构造函数是否必须在C ++中初始化成员变量? - Do constructors have to initialize member variables in C++? 如何在子类中初始化静态常量成员变量? - How do I initialize static constant member variables in a subclass? 如何初始化无法访问的模板类的静态成员? - How do you initialize a static member of an inaccessible template class? 在联合本身初始化后如何初始化联合成员? - How do you initialize a union member after the union itself was initialized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM