2017-10-04 126 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