1.C++新手在指定結構成員時,不知道何時用.運算符,何時是用->運算符。

結論:如果結構標識符是結構名,則使用句點運算符;如果標識符是指向結構的指針,則使用箭頭運算符。
#include <iostream> struct inflatable { char name[20]; float volume; double price; }; int main(){ using namespace std; int a; //僅為保持dos界面 inflatable *ps=new inflatable; cout<<"Enter name of inflatable item: "; cin.get(ps->name,20 ); cout<<"Enter volume in cubic feet: "; cin>>(*ps).volume; cout<<"Enter price : $"; cin>>ps->price; cout<<"Name: "<<(*ps).name<<endl; cout<<"Volume: "<<ps->volume<<"cubic feet\n"; cout<<"Price: $"<<ps->price<<endl; delete ps; cin>>a; //僅為保持dos界面 return 0; }
輸出結果:

對於例子中的 *ps 這個結構指針:
ps->name 等價於 (*ps).name
2.new創建的對象不是用“*”或“.”來訪問該對象的成員函數的,而是用運算符“->”
Rec *rec=new Rec(3,4); rec->getArea(); delete rec;
C++用new創建對象時返回的是一個對象指針,用new 動態創建的對象必須用delete來撤銷該對象。只有delete對象才會調用其析構函數。
注意:new創建的對象不是用“*”或“.”來訪問該對象的成員函數的,而是用運算符“->”;
總結:指針對象調用的方法都要用 ->
