c++ 指针

#include <iostream>

using namespace std;

class AHasPtr {
public:
  AHasPtr(int *p, int i):ptr(p), val(i) {}

  int getVal()
  {
    return this->val;
  }

  int *getPtr()
  {
    return this->ptr;
  }

  void setVal(int i)
  {
    this->val = i;
  }

  void setPtr(int *ptrP)
  {
    this->ptr = ptrP;
  }

  int get_ptr_val()
  {
    return *ptr;
  }

  void set_ptr_val(int val)
  {
    *ptr = val;
  }

private:
  int val;
  int *ptr;
};

void test_AHasPtr()
{
  int i = 42;
  AHasPtr p1(&i,42);

  cout <<"p1:"<< p1.get_ptr_val()<< endl;
  AHasPtr p2 = p1;
  cout << "p2:" << p2.get_ptr_val() << endl;

  p1.set_ptr_val(10);
  cout << "p1:" << p1.get_ptr_val() << endl;
  cout << "p2:" << p2.get_ptr_val() << endl;


}

int main()
{
  test_AHasPtr();


  system("pause");
  return 0;
}

----------------------------------------------------------------------------------------------------

p1:42
p2:42
p1:10
p2:10
请按任意键继续. . .

 

posted @ 2019-06-05 09:47  西北逍遥  阅读(148)  评论(0编辑  收藏  举报