2017-10-04 124 views
1

注意 - 我搜索了很多关于C++的指针和引用。 我似乎并不了解他们在这个特殊情况。因此张贴在这里!在这段C++代码中指针的必要性是什么?

以下是正确代码。这是工作。我写了这个在线C++实践问题。最初给我一部分代码。

我不理解为什么人对象数组被在主函数创建了一个带有*每个[n]的,如下所示:

#include <cmath> 
#include <cstdio> 
#include <vector> 
#include <iostream> 
#include <algorithm> 
using namespace std; 

class Person { 
     string name; 
     int age; 
    public: 
     Person(){ 
      name = ""; 
     } 
     virtual void putdata() = 0; 
     virtual void getdata() = 0; 
}; 


class Professor: public Person { 
     int publications, cur_id; 
     string name; 
     int age; 
    public: 
     static int professorCount; 

     Professor(){ 
      name = ""; 
      age = 0; 
      publications = 0; 
      cur_id = professorCount + 1; 

      professorCount++; 
     } 
     void getdata(){ 
      cin >> name >> age >> publications; 
     } 
     void putdata(){ 
      cout << name << " " << age << " " << publications << " " << cur_id << endl; 
     } 
}; 

class Student: public Person { 
     int marks[6]; 
     int cur_id; 
     string name; 
     int age; 
    public: 
     static int studentCount; 

     Student(){ 
      name = ""; 
      age = 0; 
      cur_id = studentCount + 1; 

      studentCount++; 
     } 
     void getdata(){ 
      cin >> name >> age >> marks[0] >> marks[1] >> marks[2] >> marks[3] >> marks[4] >> marks[5]; 
     } 
     void putdata(){ 
      cout << name << " " << age << " " << marks[0] + marks[1] + marks[2] + marks[3] + marks[4] + marks[5] << " " << cur_id << endl; 
     } 
}; 

int Professor::professorCount = 0; 
int Student::studentCount = 0; 

在低于该主功能,阵列Person对象正在创建,但它在开始时被赋予*。它是如何工作的?

int main(){ 

    int n, val; 
    cin>>n; //The number of objects that is going to be created. 
    Person *per[n]; // THIS ONE RIGHT HERE! THIS ONE! 

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

     cin>>val; 
     if(val == 1){ 
      // If val is 1 current object is of type Professor 
      per[i] = new Professor; 

     } 
     else per[i] = new Student; // Else the current object is of type Student 

     per[i]->getdata(); // Get the data from the user. 

    } 

    for(int i=0;i<n;i++) 
     per[i]->putdata(); // Print the required output for each object. 

    return 0; 

} 
+1

就这样可以填充阵列满或内存泄漏是方式更方便,更不容易出错。 – juanchopanza

+0

@juanchopanza - 请你详细说明一下。谢谢! –

+0

标识使用的变量长度数组(VLA)不支持*标准* C++,但某些编译器添加为扩展。转换为'std :: vector'或使用动态内存分配。 –

回答

1

你正在创建的指针到Person对象的数组。这就是per[i] = new Professor;这样的任务可以工作 - new Professor返回一个指向Professor对象的指针,所以你需要一个指针数组来存储它。

+2

嗨!这完全打击了我的想法 - 我不知道那位新教授回复了一个教授对象的指针!非常感谢! –

+0

@NarayanDheerajKumar如果您目前的阅读材料让您感到惊讶,我强烈建议您获取[更好的教科书](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) 。 – user4581301

+0

@ user4581301我会这么做的。 –

3

我不理解为什么人对象数组正在与*per[n]主函数创建如下所示

存储指针的目的是为了支持虚拟多态性(像Person这样的抽象类不能被实例化)。一个智能指针也可以提供,但需要注意正确的dynamic memory management

根本不需要在C++中使用原始指针或原始数组。该代码没有给出“最佳实践”的好例子。

main()功能

Person *per[n]; // Note that VLA's aren't standard c++ syntax 

应与

std::vector<std::unique_ptr<Person>> per(n); 

并相应环路

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

    cin>>val; 
    if(val == 1){ 
     // If val is 1 current object is of type Professor 
     per[i] = std::make_unique<Professor>(); 
       // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    } 
    // Else the current object is of type Student 
    else per[i] = std::make_unique<Student>(); 
       // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

    per[i]->getdata(); // Get the data from the user. 

} 

另外

int marks[6]; 
被替换

应与

std::array<int,6> marks; 

std::array被替换时为功能参数传递等

+0

非常感谢。我会牢记这一点。 –

+0

虽然这是真的,但我不认为这解决了为什么要存储指向对象的指针而不是对象本身的关键问题。 – templatetypedef

+1

@templatetypedef我想我的同伴用户呢。段落以“存储指针的目的是为了支持虚拟多态性...” – user4581301