当前位置:网站首页>Object array and object pointer
Object array and object pointer
2022-04-23 06:37:00 【*Flowers bloom on the street】
One 、 An array of objects
Will have Objects of the same class type An orderly collection constitutes An array of objects .
Definition form of one-dimensional object array :
Class name Object array name [ Constant expression ];
Such as : student stu1[10];
Object array instructions :
1) When creating an object array , Constructor needs to be called . If the object array has 10 Elements , You need to call 10 The secondary constructor .
2) If the constructor has only one parameter , When defining an array, you can directly provide arguments in the initial value list
Such as :student stu1[3] = {67,75,89 };
3) If the constructor has multiple arguments , You cannot provide all arguments directly when defining an array , You should write out the constructor name separately and specify the arguments in parentheses , Call constructors separately , Initialize each element pair .
Such as :student stu1[3]={student(20060101,” Zhang San ”, 67),student(20060102,” Li Si ,75),student(20060101,” Wang Wu ”,75) };
Two 、 Object pointer
C++ The pointer of is divided into Data pointer 、 A function pointer 、 Data member pointer 、 Member function pointer Four kinds of , and You can't just switch between each other . The first two are C Linguistic , It's called a normal pointer ; The latter two are C++ Specially extended for classes , Called a member pointer . become Member pointer Related to the type of class and the type of member , it Applies only to non static members of a class . Because static class members are not part of any object , So static member pointers can be ordinary pointers .
1> Pointer to object
When creating objects , The compiling system will allocate a certain storage space for each object , To deposit its members . The starting address of the object memory unit is the pointer of the object .
You can define a pointer variable , The address used to store the object , This is it. Pointer variable to the object .
The definition form of pointer variable pointing to class object :
Class name * Object pointer variable name ;
Such as : student stu;
student *p = &stu;
You can access objects and their members through object pointers .
Such as : student stu;
student *p = &stu;
p -> num ; or (*p). num;
p -> display(); or (*p). display( );
2> A pointer to a member of an object
The members of the object take up storage space , So there are addresses , Can define Pointer variables to object members .
1. Pointer variable definition form pointing to object data member :
Data member type * Pointer variable name ;
Such as : student stu;
int *p = &stu . num;
2. Pointer to object member function
When defining a pointer to a member function , You must ensure that it matches the type of the function it refers to :
1) Type and number of function parameters .
2) Return type .
3) The type of class .
Pointer variable definition form pointing to object member function :
data type ( Class name :* Pointer variable name )( parameter list );
Such as : void ( student :: *p)();
p= &student :: display;
3> Pointing to the current object this The pointer
When member functions of different objects refer to data members , How to ensure that the specified member is referenced ?
Each member function contains a special pointer , The name of this pointer is fixed , be called this The pointer , It's a pointer to an object of this class , Its value is The starting address of the object where the currently called member function is located .
this Pointer is a Implied pointer , Implicit in In the member function of each class , That is, when a member function is called , All will Automatically generate One this The pointer , It is Passed as a parameter to a member function .
#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;
}
版权声明
本文为[*Flowers bloom on the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547207176.html
边栏推荐
- 小区房价可视化
- 深拷贝和浅拷贝的区别
- 1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
- Make your own small program
- A solution to replace not in in SQL
- NVIDIA Jetson: GStreamer 和 openMAX(gst-omx) 插件
- Swagger2 generates API documents
- Arcpy为矢量数据添加字段与循环赋值
- 【踩坑】Win11 WSL2 中 meld 无法正常使用问题修复
- Rust:如何 match 匹配 String 字符串?
猜你喜欢
随机推荐
深拷贝和浅拷贝的区别
爬虫效率提升方法
OpenCV使用 GenericIndex 进行 KNN 搜索
【UDS统一诊断服务】四、诊断典型服务(2)— 数据传输功能单元
【UDS统一诊断服务】四、诊断典型服务(6)— 输入输出控制单元(0x2F)
SVN简单操作命令
C语言实现2048小游戏方向合并逻辑
大学概率论与数理统计知识点详细整理
共用数据的保护
MySQL groups are sorted by a field, and the first value is taken
Make your own small program
四元数乘法
Graduation project, viewing screenshots of epidemic psychological counseling system
定位器
相机标定:关键点法 vs 直接法
Feign请求日志统一打印
函数的调用过程
线程和进程的关系和区别是什么
队列解决约瑟夫问题
NVIDIA Jetson: GStreamer 和 openMAX(gst-omx) 插件