提问人:ScatterBrainer 提问时间:11/12/2020 最后编辑:ScatterBrainer 更新时间:11/12/2020 访问量:226
尝试将指向对象的指针数组传递到 C++ 中另一个类的函数(不允许使用向量)
Trying to pass an array of pointers to objects, to a function of another class in C++ (not allowed to use vectors)
问:
我有一个名为 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
}
关于如何将指针传递给所有学生而不仅仅是一个学生的任何想法。我再次尝试通过引用传递数组。思潮?
答:
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;
}
评论
std::vector
pointer_array[stc] = &student;
存储指向 的本地副本的指针 - 考虑到您在循环中调用函数,它始终具有相同的地址也就不足为奇了Student
new
delete
new
Student* student
Student
*(students[i])
students[i]