Dynamically allocate memory for struct

前端 未结 8 612
自闭症患者
自闭症患者 2020-12-31 12:21

I am taking a C++ class and have a assignment which requires me to dynamically allocate memory for a struct. I don\'t recall ever going over this in class and we only brief

相关标签:
8条回答
  • 2020-12-31 12:53

    new returns a pointer to an object... so you'd want

    struct Student* student1 = new Student;
    
    0 讨论(0)
  • 2020-12-31 12:57

    Why are you using new? Just declare an instance of the variable:

    Student student1;
    

    Given the definition of Student, it doesn't look like it has identity, and it is certainly copyable, so you should probably never new it.

    (I'd also provide it with a constructor, so that it can be initialized correctly from the moment it is defined.)

    0 讨论(0)
提交回复
热议问题