简体   繁体   English

C ++类成员初始化两次

[英]C++ class member initialized twice

Are there two MyVec 's being created? 是否有两个MyVec被创建? Does a temporarty, default constructed, MyVec exist before the assinment in Foo 's constructor? Foo的构造函数中, MyVec存在一个临时构造,默认构造, MyVec存在吗?

struct Foo {

  typedef std::vector<int> MyVec;

  Foo () {

    // determine how big the vector needs to be.
    // .....

    m_vec = MyVec(12);
  }

  MyVec m_vec; 
};

I know I can do it using pointers. 我知道我可以用指针做到这一点。

struct Foo {

  typedef std::vector<int> MyVec;

  Foo () {
    m_vec = new MyVec();
  }

  ~Foo () {
    delete m_vec;
  }

  MyVec * m_vec;
};

But I'd like to avoid that if possible. 但是如果可能的话,我想避免这种情况。

Edit: 编辑:

I forgot to mention. 我忘了提。 I can't use initializer lists because I need to do stuff in the constructor before the assignment. 我不能使用初始化列表,因为我需要在赋值之前在构造函数中做一些事情。

Try this syntax instead: 请尝试使用以下语法:

struct Foo {

  typedef std::vector<int> MyVec;

  Foo ()
  : m_vec(12)
  {
  }

  MyVec m_vec; 
};

It's called a c++ member initialization list . 它被称为c ++成员初始化列表


I can't use initializer lists because I need to do stuff in the constructor before the assignment. 我不能使用初始化列表,因为我需要在赋值之前在构造函数中做一些事情。

If you need to calculate how big the vector is, perhaps you can do that either before you call the constructor, or in a static method which you call from the constructor: 如果你需要计算向量的大小,也许你可以在调用构造函数之前或在从构造函数调用的静态方法中执行此操作:

struct Foo {

  typedef std::vector<int> MyVec;

  Foo ()
  : m_vec(calculateVectorSize())
  {
  }

  static int calculateVectorSize()
  {
      // determine how big the vector needs to be.
      // .....
  }

  MyVec m_vec; 
};

If those are impossible too, then a cheaper solution than your original post is: 如果那些也是不可能的,那么比原始帖子更便宜的解决方案是:

struct Foo {

  typedef std::vector<int> MyVec;

  Foo () {

    // determine how big the vector needs to be.
    // .....

    m_vec.resize(12);
  }

  MyVec m_vec; 
};

Yes. 是。

And yes you can avoid it: 是的,你可以避免它:

Foo():
  m_vec(/*args_to_construct_m_vec*/)
{
  // body goes here
}

via the above syntax. 通过上面的语法。

If you cannot use initializer list because you want to do work what to put into m_vec , another approach is to split your class. 如果你不能使用初始化列表,因为你想做什么工作放入m_vec ,另一种方法是拆分你的类。

The part of the class that needs to be initialized before m_vec 's size is calculated gets stuck into a parent class. 在计算m_vec的大小之前需要初始化的类的部分将被卡入父类。 You construct it, then construct m_vec in the initializer list. 您构造它,然后在初始化列表中构造m_vec

Note that std::vector default initialization is pretty damn cheap (usually " memset " sizeof ( 3 pointers ) to 0 ), so your concern is almost certainly misplaced. 请注意, std::vector默认初始化非常便宜(通常是“ memsetsizeof (3指针)为0 ),因此您的担忧几乎肯定是错位的。

I believe the typedef is just telling the compiler that MyVec can be seen as vector<int> gvien that the vector library is included. 我相信typedef只是告诉编译器MyVec可以被视为包含矢量库的vector<int> MyVec

However, no actual MyVec is constructed yet. 但是,还没有构建实际的MyVec

Also you should try the alternative and more preferred syntax for initializing member fields. 您还应该尝试使用替代和更首选的语法来初始化成员字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM