在C++; 我对C++有点生疏了,暂时没有用过。所以我需要制作一个数据结构,它可以保存对象并对它们进行操作。就像一个数组列表。现在,我首先创建并初始化我的类类型的动态数组: Student* array_of_students = new Student[SIZE];

在C++; 我对C++有点生疏了,暂时没有用过。所以我需要制作一个数据结构,它可以保存对象并对它们进行操作。就像一个数组列表。现在,我首先创建并初始化我的类类型的动态数组: Student* array_of_students = new Student[SIZE];,c++,C++,然后我创建一个Student对象,如下所示: Student student(param1, param2, etc..); 我还提供了一个析构函数,因为我使用了很多char指针(我知道我应该使用string,但这只是为了学习),在这里我删除了一些包含一些名称的指针 ~Student(){ /* delete filed1; etc */ } 当我尝试列出这些名称时,我会得到一些随机值,可能是因为指针被删除了,这意味着调用了析构函数,即使我使用array\u of_students引用了一个

然后我创建一个Student对象,如下所示:

Student student(param1, param2, etc..);
我还提供了一个析构函数,因为我使用了很多char指针(我知道我应该使用string,但这只是为了学习),在这里我删除了一些包含一些名称的指针

~Student(){ /* delete filed1; etc */ }
当我尝试列出这些名称时,我会得到一些随机值,可能是因为指针被删除了,这意味着调用了析构函数,即使我使用
array\u of_students
引用了一个特定的对象,这些字段也会被销毁,我没有任何显示。这可能是因为
Student
实例超出范围并调用其析构函数

那么这里的解决方案是什么呢?只要
array\u学生
还活着,我怎么能拿着它们

我认为我需要定义某种复制构造函数,将原始学生实例复制到新副本 这将被学生的数组引用。你的想法会有帮助的

编辑:

class Student{

private :
   char *_name;

   public :

   Student(){}
   Student(char* name){

      _name = new char[strlen(name)];
      strcpy(_name, name); }

   char* getNaziv(){return _name;}
   ~Student(){delete _name;}

};

int main()
{
    Student* array_of_students = new Student[5];
    char input[100];
    for(int i = 0; i < 5; i++){
        cout << "Input name : "<<endl;
        cin >> input;
        Student tmp(input);
        array_of_students[i] = tmp;
    }

    for(int i = 0; i < 5; i++){

        cout <<"Name is : "<< array_of_students[i].getNaziv()<< endl;
    }
}
班级学生{
私人:
字符*\u名称;
公众:
学生(){}
学生(字符*名称){
_名称=新字符[strlen(名称)];
strcpy(_name,name);}
char*getNaziv(){return\u name;}
~Student(){删除_name;}
};
int main()
{
学生*学生数组=新学生[5];
字符输入[100];
对于(int i=0;i<5;i++){

CUT

尽量不要用字符数组来存储字符串,而是使用<代码> STD::String >,如果可能,不要使用<代码>新< /Cord>>关键字来分配内存,让C++负责分配和分配内存。 不要使用

学生的原始数组
,最好使用
std::vector
或其他容器类。如下所示:

#include <cstdlib>
#include <iostream>
#include <vector>

class Student
{
        private :
                std::string _name;

        public :
                Student ( );
                Student ( std::string& name );

                void name ( std::string& );
                std::string& name ( );
};

Student::Student ( )
{
        /* Constructor with no parameters, do some crazy stuff... */
}

Student::Student ( std::string& name )
{
        this -> name ( name );
}

void Student::name ( std::string& name )
{
        this -> _name = name;
}

std::string& Student::name ( )
{
        return ( this -> _name );
}

int main ( int argc, char **argv )
{
        std::vector<Student> array_of_students;
        std::string input;

        for(int i = 0; i < 5; i++)
        {
            std::cout << "Input name : " << std::endl;
            std::cin >> input;

            Student tmp ( input );
            array_of_students.push_back ( tmp );
        }

        for( auto sit = array_of_students.begin ( ); sit != array_of_students.end ( ); ++ sit )
        {
                std::cout <<"Name is : "<< ( *sit ).name ( ) << std::endl;
        }

        return ( EXIT_SUCCESS );
}

尽量不要使用字符数组来存储字符串,而是使用<代码> STD::String 。如果可能,不要使用<代码>新< /Cord>>关键字来分配内存,让C++负责分配和分配内存。 不要使用

学生的原始数组
,最好使用
std::vector
或其他容器类。如下所示:

#include <cstdlib>
#include <iostream>
#include <vector>

class Student
{
        private :
                std::string _name;

        public :
                Student ( );
                Student ( std::string& name );

                void name ( std::string& );
                std::string& name ( );
};

Student::Student ( )
{
        /* Constructor with no parameters, do some crazy stuff... */
}

Student::Student ( std::string& name )
{
        this -> name ( name );
}

void Student::name ( std::string& name )
{
        this -> _name = name;
}

std::string& Student::name ( )
{
        return ( this -> _name );
}

int main ( int argc, char **argv )
{
        std::vector<Student> array_of_students;
        std::string input;

        for(int i = 0; i < 5; i++)
        {
            std::cout << "Input name : " << std::endl;
            std::cin >> input;

            Student tmp ( input );
            array_of_students.push_back ( tmp );
        }

        for( auto sit = array_of_students.begin ( ); sit != array_of_students.end ( ); ++ sit )
        {
                std::cout <<"Name is : "<< ( *sit ).name ( ) << std::endl;
        }

        return ( EXIT_SUCCESS );
}

C++(java)的等价语言数组列表是。请不要使用原始数组:)请提供一个SSCCE。@nhrnjic6谁不允许?上帝?你的妈妈?没有理由,我就关闭它作为主题。也许至少可以使用智能指针来管理这些原始数组?你也不必使用
new
来创建对象。
Student array\u of\u students[SIZE]工作很好。请不要在C++中编写java,它不能工作。C++相当于(java的)数组列表是。请不要使用原始数组:)请提供一个SSCCE。@nhrnjic6谁不允许?上帝?你的妈妈?没有理由,我就关闭它作为主题。也许至少可以使用智能指针来管理这些原始数组?你也不必使用
new
来创建对象。
Student array\u of\u students[SIZE]工作很好。请不要在C++中编写java,它不能工作。