动态对象数组在C++和java中的不同?

动态对象数组在C++和java中的不同?,java,c++,arrays,Java,C++,Arrays,我最近学习了Java中的对象数组。 考虑这一点: Student s[]=new student[3]; 但是为什么上面的语句只是创建了一个数组,它可以保存对3个学生对象的引用。它本身并不创建学生类对象。我必须使用Student类的构造函数分别创建它们。因此,如果我尝试访问任何类似这样的类成员: s[0]。名称=满足 它将生成运行时错误NullPointerException 但是这些对我来说很奇怪,因为在C++中这些是不需要的。 考虑这个C++程序。< /P> #include <io

我最近学习了Java中的对象数组。 考虑这一点:

Student s[]=new student[3];
但是为什么上面的语句只是创建了一个数组,它可以保存对3个学生对象的引用。它本身并不创建学生类对象。我必须使用Student类的构造函数分别创建它们。因此,如果我尝试访问任何类似这样的类成员:

s[0]。名称=满足

它将生成运行时错误NullPointerException

但是这些对我来说很奇怪,因为在C++中这些是不需要的。 考虑这个C++程序。< /P>
#include <iostream>
using std::cout;
class Test
{
    public:
        Test()
        {
            cout<<"Constructor\n";
        }
        void fun()
        {
            cout<<"fun() is called\n";
        }
        ~Test()
        {
            cout<<"Destructor\n";
        }
};
int main()
{
    Test* t=new Test[3];
    for(int i=0;i<3;i++)
        t[i].fun();
    delete[] t;
    return 0;
}
当main中的第一条语句执行时,内部会发生什么?如果它在C++中工作得很好,那么为什么在java中生成nulpPOutExtExchange。
   class test
    {
          test()
          {
                 System.out.println("Constructor");
          }

          void fun()
          {
                 System.out.println("fun() is called");
          }
           public static void main(String args[])
           {
                 test t[]=new test[3];
                 for(int i=0;i<3;i++)
                       t[i].fun();  // oops runtime error
                 for(int i=0;i<3;i++)
                       t[i]=new test();
                 for(int i=0;i<3;i++)
                       t[i].fun();  // now fine.
           }

}
为什么对象必须使用java中的类构造函数单独创建,但它们不需要用C++中的类构造函数单独创建?p>
请帮帮我。

简短的回答只是因为语法几乎相同并不意味着它的工作原理相同。事实上,正如你写的那样,语句在C++中是不编译的,因为学生[[]/P>中的[] ]。 <>你的java代码的C++代码是:

Student** s = new Student*[3];
然后可以像这样初始化学生:

s[0]=新学生吉米

这是一个重要的概念,因为java不是唯一的C++语言,它与C++不同。 这里有一个完整的例子,你可以玩玩。用g++4.3.2编译

#include <iostream>
#include <string>

class Student {
public:
    Student() : name_("<unknown>") {
        std::cout << "Default constructor called" << std::endl;
    }
    Student(const char* name) : name_(name) {
        std::cout << "constructor called with name '" << name << "'" << std::endl;
    }
    void setName(const std::string& name) { name_ = name; }
    const std::string& getName() const { return name_; }

private:
    std::string name_;
};

void demoArrayOfValues() {
    std::cout << "demoArrayOfValues()" << std::endl;

    // Here the default constructor will be called 3 times because
    // creating you're creating an array of objects
    Student* s =  new Student[3];

    for (int x = 0; x < 3; ++x) {
        std::cout << "Student[" << x << "] (byValue): " << s[x].getName() << std::endl;
    }

    s[0].setName("Jimmy");
    s[1].setName("Sally");
    s[2].setName("Susie");

    for (int x = 0; x < 3; ++x) {
        std::cout << "Student[" << x << "] (after setting): " << s[x].getName() << std::endl;
    }

    std::cout << std::endl;
}

void demoArrayOfPointers() {
    std::cout << "demoArrayOfPointers()" << std::endl;

    // This is the C++ equivalent of the Java example that started the question
    // Here you're creating an array of pointers and have to create the objects
    // themselves
    Student** s =  new Student*[3];

    for (int x = 0; x < 3; ++x) {
        std::cout << "Student[" << x << "] (pointer): " << s[x] << std::endl;
    }

    s[0] = new Student("Jimmy");
    s[1] = new Student("Sally");
    s[2] = new Student("Susie");

    for (int x = 0; x < 3; ++x) {
        std::cout << "Student[" << x << "] (after creating): " << s[x]->getName() << std::endl;
    }

    std::cout << std::endl;

    // Now we iterate over the elements in the array and delete each one.       
    for (int x = 0; x < 3; ++x) {
        delete s[x];
    }

    // Now we delete the array itself
    delete [] s;
}

int main(int argc, const char** argv) {
    demoArrayOfValues();
    demoArrayOfPointers();

    return 0;
}

java不是C++,为什么你期望它以同样的方式工作?有很多不同之处,也有一些相似之处。最好理解每个语言中数组的语义。这可能对你有帮助:java数组不包含C++类的类实例。如果C++中有一个等价的间接指向,即指针数组,那么你必须明确地创建对象。学生**s=新学生*[3];如何使用delete运算符为上述语句释放内存?当然。s是一个动态分配的数组,包含指向学生实例的3个指针。最初,每个数组元素都是无效指针。因此,您必须将每个元素设置为一个新实例,例如s[0]=new StudentJimmy;。我将修改我的示例以正确删除这些对象。在本例中,首先要删除学生实例,然后使用delete[]删除指针数组。我在VC++中尝试了这个示例&它向我显示的是垃圾值,而不是0作为输出。为什么?好问题。分配数组时,指针值未定义。这与空PTR不同。