C++新手上路第六天,指针

#include "stdafx.h"

#include<iostream>

using namespace std;

 

int main()

{

int i;//定义一个int变量i

int *ptr = &i;//再定义一个指针变量ptr,并将i取的地址赋值给ptr

i = 5;//给i赋值

cout << "i= " << i << endl;//输出i的值,肯定是5

cout << "*ptr= " << *ptr << endl;//输出ptr所指向地址内的内容,也就是i的值

cout << "ptr= " << ptr << endl;//输出ptr指向的地址

cout << "&i"<<&i << endl;//输出i的地址,&为取地址符

    return 0;

}

 

//指针数组

#include "stdafx.h"

#include<iostream>

using namespace std;

 

int main()

{

int line1[] = { 1,0,0 };//定义数组,矩阵的第一行

int line2[] = { 0,1,0 };;//定义数组,矩阵的第二行

int line3[] = { 0,0,1 };;//定义数组,矩阵的第三行

 

//定义整型指针数组并初始化

int *pLine[3] = { line1,line2,line3 };

 

cout << "Matrix test:" << endl;

for (int i = 0; i < 3; i++) {//对指针数组元素的循环

for (int j = 0; j < 3; j++)//对矩阵每一行循环

cout << pLine[i][j] << " ";

cout << endl;

}

    return 0;

}

 

对象指针

#include "stdafx.h"

#include<iostream>

using namespace std;

 

class Point

{

public:

Point(int x = 0, int y = 0) :x(x), y(y) {}

int getX()const { return x; }

int getY()const { return y; }

 

private:

int x, y;

};

 

int main()

{

Point a(4, 5);

Point *p1 = &a;//这里指针的类型是Point,取a的地址

cout << p1->getX() << endl;//利用指针对象访问对象成员

cout << a.getX() << endl;

    return 0;

}

 

函数指针

#include "stdafx.h"

#include<iostream>

using namespace std;

 

void printStuff(float) {

cout << "This is the print stuff function." << endl;

}

void printMessage(int data) {

cout << "The data to be listed is " << data << endl;

}

void printFloat(float data) {

cout << "The data to be printed is " << data << endl;

}

const float PI = 3.14159f;

const float TWO_PI = PI * 2.0f;

const int myInt = 4;

 

int main()

{

void(*functionPointer)(float);//声明指针函数

printStuff(PI);

functionPointer = printStuff;//指针指向printStuff

functionPointer(PI);//函数指针调用

functionPointer = printFloat;//函数指向PrintFloat

functionPointer(TWO_PI);//函数指针调用

printFloat(TWO_PI);

    return 0;

}

 

//通过指针来访问类的静态成员

#include "stdafx.h"

#include<iostream>

using namespace std;

 

class Point

{

public:

Point(int x = 0, int y = 0) :x(x), y(y) {

count++;

}

Point(const Point &p) :x(p.x), y(p.y) {

count++;

}

~Point() { count--; }

int getX()const { return x; }

int getY()const { return y; }

static int count;

static void showCount() {

cout << "  Object count = " << count << endl;

}

 

private:

int x, y;

};

 

int Point::count = 0;//对count进行初始化

 

int main()

{

int *ptr = &Point::count;//定义指针指向Point类的静态数据成员count

Point a(4, 5);

cout << "Point A: " << a.getX() << "," << a.getY();

cout << "  Object count = " << *ptr << endl;

Point b(a);

cout << "Point B: " << b.getX() << "," << b.getY();

cout << "   Object count = " << *ptr << endl;

void(*funcPtr)() = Point::showCount;//定义指针指向Point类的静态函数成员showCount

funcPtr();//函数指针的调用

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值