当前位置:网站首页>对象数组与对象指针
对象数组与对象指针
2022-04-23 05:48:00 【*陌上花开】
一、对象数组
将具有相同类类型的对象有序地集合在一起便构成了对象数组。
一维对象数组定义形式:
类名 对象数组名[常量表达式];
如: student stu1[10];
对象数组使用说明:
1)在建立对象数组时,需要调用构造函数。如果对象数组有10个元素,就需要调用10次构造函数。
2)如果构造函数只有一个参数,在定义数组时可以直接在初值列表中提供实参
如:student stu1[3] = {67,75,89 };
3)如果构造函数有多个参数,则不能在定义数组时直接提供所有实参,应分别写出构造函数名并在括号内指定实参,分别调用构造函数,对每个元素初始化。
如:student stu1[3]={student(20060101,”张三”, 67),student(20060102,”李四,75),student(20060101,”王五”,75) };
二、对象指针
C++的指针被分成数据指针、函数指针、数据成员指针、成员函数指针四种,而且不能随便相互转换。其中前两种是C语言的,称为普通指针;后两种是C++专门为类扩展的,称为成员指针。成员指针与类的类型和成员的类型相关,它只应用于类的非静态成员。由于静态类成员不是任何对象的组成部分,所以静态成员指针可用普通指针。
1>指向对象的指针
在建立对象时,编译系统会为每一个对象分配一定的存储空间,以存放其成员。对象内存单元的起始地址就是对象的指针。
可以定义一个指针变量,用来存放对象的地址,这就是指向对象的指针变量。
指向类对象的指针变量定义形式:
类名*对象指针变量名;
如: student stu;
student *p = &stu;
可以通过对象指针访问对象和对象的成员。
如: student stu;
student *p = &stu;
p -> num ; 或 (*p). num;
p -> display(); 或 (*p). display( );
2>指向对象成员的指针
对象的成员要占用存储空间,因此也有地址,可以定义指向对象成员的指针变量。
1.指向对象数据成员的指针变量定义形式:
数据成员类型 *指针变量名;
如: student stu;
int *p = &stu . num;
2.指向对象成员函数的指针
定义成员函数的指针时,必须确保与它所指函数的类型相匹配:
1)函数形参的类型和数目。
2)返回类型。
3)所属类的类型。
指向对象成员函数的指针变量定义形式:
数据类型(类名:*指针变量名)(参数列表);
如: void ( student :: *p)();
p= &student :: display;
3>指向当前对象的this指针
当不同对象的成员函数引用数据成员时,怎么保证引用的是所指定的成员呢?
在每一个成员函数中都包含了一个特殊的指针,这个指针的名字是固定的,称为this指针,它是指向本类对象的指针,它的值是当前被调用的成员函数所在对象的起始地址。
this指针是一种隐含指针,隐含于每个类的成员函数中,即调用某成员函数时,都将自动产生一个this指针,它是作为参数被传递给成员函数的。
#include<iostream>
using namespace std;
class Student
{
private:
int num;
string name;
float score;
public:
Student()
{
num = 2020;
name = "zhangsan";
score = 85;
}
void Print()
{
cout << "num:" << this->num << endl;
cout << "name:" << (*this).name << endl;
cout << "score:" << score << endl;
}
};
int main()
{
Student stu1;
stu1.Print();
return 0;
}
版权声明
本文为[*陌上花开]所创,转载请带上原文链接,感谢
https://blog.csdn.net/swint_er/article/details/123409817
边栏推荐
- Basic knowledge of network in cloud computing
- Detection technology and principle
- P1586 solution to tetragonal theorem
- MySQL table constraints and table design
- Flask - 中间件
- 识别验证码
- 爬取彩票数据
- C语言输入和输出(printf和scanf函数、putchar和getchar函数)
- MySQL advanced query
- xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied问题
猜你喜欢
scikit-learn sklearn 0.18 官方文档中文版

Export the articles written in CSDN to PDF format

Definition of C class and method

MySQL groups are sorted by a field, and the first value is taken

Cf1427c the hard work of paparazzi

P1586 solution to tetragonal theorem

C#中?的这种形式

Addition, deletion, modification and query of MySQL table

Robocode教程4——Robocode的游戏物理

A solution to replace not in in SQL
随机推荐
LockSupport. Park and unpark, wait and notify
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
爬取手游网站游戏详情和评论(MQ+多线程)
檢測技術與原理
Rust:如何 match 匹配 String 字符串?
Mysql database foundation
H. Are You Safe? Convex hull naked problem
生成验证码
[leetcode 228] summary interval
The most practical chrome plug-in
SQL sorts according to the specified content
How SYSTEMd uses / etc / init D script
Rust 中的 Cell 共享可变指针
pyppeteer爬虫
爬取蝉妈妈数据平台商品数据
Rust的闭包类型(Fn, FnMut, FnOne的区别)
Kalman filter and inertial integrated navigation
GDB debugger installation and use
Integers have friends interval GCD + double pointer
Arcpy为矢量数据添加字段与循环赋值