简体   繁体   English

在类内声明的C ++ Struct

[英]C++ Struct declared inside a class

Long time reader first time poster. 长期的读者第一次海报。 Searching for this is very obscure so I couldn't find anything. 搜索这个内容很晦涩,所以我什么也找不到。

Anywho, if I declare a struct inside of a class. 任何人,如果我在类中声明结构。 Then I have a class method that creates instances of said struct, what is the lifespan of the created struct instance? 然后,我有一个创建该struct实例的类方法,创建的struct实例的寿命是多少?

For instance in my header: 例如在我的标题中:

class test_class
{
     Public:
          void test_function(int, string);

     private:
          struct test_struct
          {
               int foo;
               string bar;
          };

          test_struct * storage;
}

Then in test_class.cpp 然后在test_class.cpp中

void test_function(int num, string name)
{
     test_struct t1;
     t1.foo = num;
     t1.bar = name;

     storage = new test_struct [<some_size>];
     storage[<some_element>] = t1;
}

So, I make an instance of my test_class and call the test_function. 因此,我创建了我的test_class的实例,并调用了test_function。 t1 is created and then stored successfully in the hypothetical array, but does it stay saved in the array? 创建了t1,然后成功地将其存储在假设的数组中,但是它仍保存在数组中吗? Or does it get deleted when the function exits because the scope shifts? 还是因为范围发生变化而在函数退出时被删除了? Does t1 become a member variable of the class? t1是否成为该类的成员变量? Would I need t1 to be a pointer to a test_struct and have an array of test_struct pointers? 我需要将t1用作指向test_struct的指针并具有一个test_struct指针数组吗?

Thank you in advance for the help. 预先感谢您的帮助。

Lifetime has nothing to do with nesting of class definition. 生命周期与类定义的嵌套无关。

This statement 这个说法

 test_struct t1;

declares a local variable whose lifetime ends at the closing right brace of the block containing the declaration. 声明一个局部变量,其生存期在包含该声明的块的右右括号处结束。 It declares a local variable because it's in a block in a function definition. 它声明局部变量,因为它位于函数定义的一个块中。

This statement 这个说法

 storage = new test_struct [<some_size>];

dynamically creates <some_size> instances of test_struct . 动态地创建<some_size>的实例test_struct The lifetime of that array ends when you delete[] it. delete[]时,该数组的生命周期结束。 If you do. 如果是这样。

This statement 这个说法

storage[<some_element>] = t1;

is a copy assignment, copying the value of t1 . 是复制分配,复制t1的值。 It doesn't create a new instance. 它不会创建新实例。


Check out the SO C++ booklist to find a C++ textbook that suits you. 查看SO C ++书籍列表 ,找到适合您的C ++教科书。 If you can, get someone with higher reputation to help you, so that you can read the deleted answers . 如果可以的话,请声誉较高的人来帮助您,以便您阅读已删除的答案 For unfortunately, the existing community effort was at one time deleted and replaced with a summary. 不幸的是,现有的社区工作一次被删除,并以摘要代替。

Looking at your question, looks like you are confused about the struct type inside the class. 看着您的问题,您似乎对类中的struct类型感到困惑。 If it helps, here's a bit of explanation with 如果有帮助,这里有一些解释

      struct test_struct
      {
           int foo;
           string bar;
      };

inside the class, you've just created a new type that's encapsulated inside the class. 在类内部,您刚刚创建了一个封装在类内部的新类型。 (Treat it like any other type). (像对待其他类型一样处理它)。 Answers to your questions 1.t1 is created and then stored successfully in the hypothetical array, but does it stay saved in the array? 对您的问题的答案1.t1已创建,然后成功存储在假设的数组中,但是它仍保存在数组中吗? Or does it get deleted when the function exits because the scope shifts? 还是因为范围发生变化而在函数退出时被删除了?

Answer: Yes it copies into the array, as mentioned in the previous answer. 答案:是的,如上一个答案所述,它会复制到阵列中。 Its a copy assignment (bit-wise copy semantics), so, your array's element now has the value copied to it from t1 它是一个副本分配(按位复制语义),因此,您数组的元素现在具有从t1复制到它的值

  1. Does t1 become a member variable of the class? t1是否成为该类的成员变量? Would I need t1 to be a pointer to a test_struct and have an array of test_struct pointers? 我需要将t1用作指向test_struct的指针并具有一个test_struct指针数组吗?

Answer: t1 is a local variable (allocated on the stack), its not a member variable of the class. 答:t1是一个局部变量(分配在堆栈上),不是类的成员变量。 With storage allocated on heap (new) you have an array of elements of test_struct, and then you simply copy the local t1's value to your array. 在堆上分配了存储空间(新)之后,您有了一个test_struct元素数组,然后只需将本地t1的值复制到您的数组中。 Essentially, you are copying the value of t1 to array before t1 is destroyed when it goes out of scope. 本质上,您要在t1超出范围时销毁t1之前将t1的值复制到数组。 You don't need to create a array to pointer to test_struct, (ofcourse if there are some compulsive reasons, in such a case you allocate memory to test_struct as well). 您无需创建指向test_struct的指针的数组(当然,如果有某些强制原因,在这种情况下,您也要为test_struct分配内存)。

your "storage" member is allocated on heap, so, make sure that you call delete[] to reclaim the memory. 您的“存储”成员分配在堆上,因此,请确保调用delete []以回收内存。

Hope it helps! 希望能帮助到你!

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

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