ポイント先 (->) 演算子とドット (.) 演算子の違いは知っていますが、なぜ 2 つの演算子が必要なのかわかりません。ポインターを使用せずにドット演算子のみを使用することは、常に同じくらい簡単ではありませんか? http://www.programcreek.com/2011/01/an-example-of-c-dot-and-arrow-usage/から
#include <iostream>
using namespace std;
class Car
{
public:
int number;
void Create()
{
cout << "Car created, number is: " << number << "\n" ;
}
};
int main() {
Car x;
// declares x to be a Car object value,
// initialized using the default constructor
// this very different with Java syntax Car x = new Car();
x.number = 123;
x.Create();
Car *y; // declare y as a pointer which points to a Car object
y = &x; // assign x's address to the pointer y
(*y).Create(); // *y is object
y->Create();
y->number = 456; // this is equal to (*y).number = 456;
y->Create();
}
なぜポインターをわざわざ使うのでしょうか? X と同じように Y を作成するだけで、同じように機能します。動的に割り当てられたメモリへのポインターが必要だと言うなら、ドット演算子をわざわざ使う必要はありません。