尝试将指向对象的指针数组传递到 C++ 中另一个类的函数(不允许使用向量)

Trying to pass an array of pointers to objects, to a function of another class in C++ (not allowed to use vectors)

提问人:ScatterBrainer 提问时间:11/12/2020 最后编辑:ScatterBrainer 更新时间:11/12/2020 访问量:226

问:

我有一个名为 Student 的类。我在主对象中创建了许多学生对象(每个对象代表一个学生。我真正想做的是让这些学生中的每一个都进入学校班级,代表学生进入学校,然后打印他/她的名字等。代码如下:

我的 study.h 文件包括:

#include <iostream>
#include <cstring>

using namespace std;
  
  class Student
    {
    private:
        string name;
        int no_floor;
        int no_classroom;
    public:
        Student(const string& nam,int no_fl,int no_cla)//constructor
        : name(nam), no_floor(no_fl), no_classroom(no_cla)
        {
          cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
        };
        ~Student() //destructor
       {
           cout << "A Student to be destroyed! with name " << name << " is at floor: " << no_floor << " class: " << no_classroom;
       };
    

然后是学校班级:

class School
       {
       private:
             Student* pointer_array[2];
       public:
             School()//constructor
             {
               cout << "A New School has been created!" << endl;
             };
             ~School(){//destructor
               cout << "A School to be destroyed!" << endl;
             };
             void enter(Student student, int stc=0/*student counter*/);
       };

在我的主 .cpp 文件上:(每个学生的内存分配)

#include <iostream>
#include <cstring>
#include <study.h>

using namespace std;
int main(void)
{
      //Student creation
   int i,floor,classroom;
   string stname;
   Student* students[2];
   for(i=0; i<2; i++)
   {
      cin >> stname;
      cin >> floor;
      cin >> classroom;
      students[i] = new Student(stname, floor, classroom);
   }



   School sch;
   for(i=0; i<2; i++)
   {
      sch.enter(*(students[i]),i);
   }

   for(i=0; i<2; i++)
   {
     delete students[i];
   }
}

最后,在我的study.cpp文件中,我得到了School类函数,我试图通过引用而不是将它们应用于新对象来传递每个对象:

#include <iostream>
#include <cstring>
#include <study.h>

using namespace std;

void School::enter(Student student, int stc/*student counter*/)
{
  pointer_array[stc] = &student;
  cout << "pointer array[" << stc <<  "]:" << pointer_array[stc] << endl; 
  //^ this cout prints the same adress for both students array[0]:0x1ffefffd20
  //                                                     array[1]:0x1ffefffd20

}

关于如何将指针传递给所有学生而不仅仅是一个学生的任何想法。我再次尝试通过引用传递数组。思潮?

C++ OOP 对象 按引用传递

评论

4赞 cigien 11/12/2020
请问你为什么要使用指针和数组?你碰巧知道什么是吗?std::vector
1赞 UnholySheep 11/12/2020
pointer_array[stc] = &student;存储指向 的本地副本的指针 - 考虑到您在循环中调用函数,它始终具有相同的地址也就不足为奇了Student
1赞 Sam Varshavchik 11/12/2020
所示代码中的问题是众所周知的问题,称为“无用使用指针”。所示代码中没有任何内容需要使用 或 。用于创建指向新对象的指针,但随后立即创建同一对象的无用副本,以便按值将其传递给另一个函数 - 这并不能真正完成任何有用的事情。newdeletenew
2赞 UnholySheep 11/12/2020
快速解决方法是将函数参数更改为直接获取指针 (),因为无论如何您已经动态分配了这些对象Student* studentStudent
1赞 Human-Compiler 11/12/2020
@ScatterBrainer代码不会仅通过更改参数来编译,因为您现在还需要传递指向函数的指针,您可以在调用 时取消引用该指针。您需要将其更改为*(students[i])students[i]

答:

1赞 ScatterBrainer 11/12/2020 #1

这是这个问题的解决方案 学校班级:

class School
{
private:
      Student* pointer_array[5];
public:
      School()//constructor
      {
        cout << "A New School has been created!" << endl;
      };
      ~School(){//destructor
        cout << "A School to be destroyed!" << endl;
      };
      void enter(Student* student, int stc=0/*student counter*/);
};

学生班级不变:

class Student
    {
    private:
        string name;
        int no_floor;
        int no_classroom;
    public:
        Student(const string& nam,int no_fl,int no_cla)//constructor
        : name(nam), no_floor(no_fl), no_classroom(no_cla)
        {
          cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
        };
        ~Student() //destructor
       {
           cout << "A Student to be destroyed! with name " << name << " is at floor: " << no_floor << " class: " << no_classroom;
       };

然后 main.cpp:

#include <iostream>
#include <cstring>
#include <study.h>

using namespace std;
int main(void)
{
      //Student creation
   int i,floor,classroom;
   string stname;
   Student* students[2];
   for(i=0; i<2; i++)
   {
      cin >> stname;
      cin >> floor;
      cin >> classroom;
      students[i] = new Student(stname, floor, classroom);
   }



   School sch;
   for(i=0; i<2; i++)
   {
      sch.enter(students[i],i);
   }

   for(i=0; i<2; i++)
   {
     delete students[i];
   }
}

最后,研究 .cpp 函数:

void School::enter(Student* student, int stc/*student counter*/)
{
  pointer_array[stc] = student;
  (pointer_array[stc])->print();
  cout << " enters school!" << endl;
  cout << "pointer array[" << stc <<  "]:" << pointer_array[stc] << endl;
}