Categories
开发

C++ 中箭头操作符(->)和点操作符(.)的不同

针对 -> 和 . 的定义区别如下:

如果 p 是指针,p -> function();

如果 p 是对象和结构体,p.function();

例如:

#include 
using namespace std;
class A{
  private:
    int a;

  public:
    A()
    {
      a = 10;
    }

    void Display()
    {
      cout << a << endl;
    }

};

int main()
{
  A *a = new A;
  a -> Display();
  A b;
  b.Display();
  delete a;
  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.