当前位置:网站首页>Discussion on the basic use and address of pointer in array object
Discussion on the basic use and address of pointer in array object
2022-07-24 15:01:00 【It's Xiaoguang a~】
Probe into the basic use and address of pointer in array object :
This involves classes and objects 、 Pointer and operator overloading :
You can refer to it before reading ( The asterisk represents the key ):
* Further understanding and use of classes and objects
** Pointer and its application
C++ Pointer to unit learning summary ( To continue ), Allocation strategy of storage space ,new and delete Operator ,const qualifier Structure ( To continue ), enumeration
***C++ Operator overload of
Some contents needing attention :
Assignment of pointer and use of address fetching character :
int *p = (int*)&b; // With a pointer p Pointing to the address b, Namely the b Assign the address to the pointer p
cout<<" The pointer p Address to :"<<&(*p)<<endl;
*p=a.b; // Give the pointer p assignment , Now pointer p It points to the address b, Therefore, it is equivalent to b=a.b;
Operator overloading :
A& operator=(const A &a){
cout<<" heavy load "<<endl;
b=a.b;
c=a.c;
return *this;
};
For operator overloading, please refer to :
***C++ Operator overload of
This part is still very interesting ( The term operator overloading basically refers to C/C++, because Java Operator overloading is not supported , Programmers cannot define + * Equal operator , There is no right for programmers to overload operators . This is also taking into account Java Object oriented programming in (OOP) A matter of principle ), Here is the function call operator in operator overloading () Some examples of :
Patients with a :
// This example refers to the rookie tutorial :https://www.runoob.com/cplusplus/function-call-operator-overloading.html
#include <iostream>
using namespace std;
class Distance
{
private:
int feet; // 0 To infinity
int inches; // 0 To 12
public:
// The required constructor
Distance(){
feet = 10;
inches = 20;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// Overloading function call operators
Distance operator()(int a, int b, int c)
{
Distance D; // Define a Distance Call the default constructor to initialize , here feet=10,inches=20;
cout<<" heavy load :\t D.displayDistance(): \t";
D.displayDistance();
// Perform random calculations ( Reassign )
D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}
// The way to show the distance
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2;
cout << "First Distance : ";
D1.displayDistance();
D2 = D1(10, 10, 10); // invoke operator()
cout << "Second Distance :";
D2.displayDistance();
return 0;
}
Example 2 :
#include <iostream>
using namespace std;
// Function object with state , Set different output stream objects and separators
class PrintString{
ostream &os;
char sep;
public:
PrintString(ostream &o = cout, char c = '\t')
: os(o), sep(c){
}
void operator()(const string& str){
os << str << sep; }
};
int main(){
PrintString welcomeMsg;
welcomeMsg("Welcome!");
PrintString errMsg(cerr, '\n');
errMsg("Error!");
errMsg("Error!");
return 0;
}

Probe into the basic use and address of pointer in array object :
#include<iostream>
#include<stdio.h>
using namespace std;
class A{
const int b;
int c;
public:
A():b(0){
/*cout<<" Call default constructor "<<endl;*/};
A(int b1,int c1):b(b1),c(c1){
};
void show(){
cout<<b<<" "<<c<<endl;
}
A& operator=(const A &a){
cout<<" heavy load "<<endl;
cout<<"b The address of :"<<&b<<endl;
int *p = (int*)&b; // With a pointer p Pointing to the address b, Namely the b Assign the address to the pointer p
cout<<" The pointer p Address to :"<<&(*p)<<endl;
*p=a.b; // Give the pointer p assignment , Now pointer p It points to the address b, Therefore, it is equivalent to b=a.b;
//b=a.b;
c=a.c;
return *this;
};
};
//A *a[100]; // The default constructor was not called , It just exists as a pointer
A a[100]; // Call default constructor , Exist as the default object
int main(){
/* const int i=2; cout<<&i<<endl; int *p = (int*)&i; cout<<&(*p)<<endl; *p=10; cout<<i<<endl; */
/* A a1 = A(100); a[0]=&a1; (*a[0]).show(); */
//A a0 = A(i+100,i+120); // If defined in this way, it is to call the constructor directly , No overloading
A a0; // This defines calling the default constructor , If the default object a0 If you make a reassignment modification, you call the overloaded constructor :a0 = A(i+100,i+120);
for(int i=0;i<10;i++){
a0 = A(i+100,i+120);
a[i]=a0;
a[i].show();
}
return 0;
}
result:
At the same time, pay attention to the address :
边栏推荐
- The spiral matrix of the force buckle rotates together (you can understand it)
- DS binary tree - maximum distance of binary tree nodes
- DS graph - minimum spanning tree
- How do novices buy stocks for the first time? Which securities company is the best and safest to open an account
- Learning rate adjustment strategy in deep learning (1)
- JS data transformation -- Transformation of tree structure and tile structure
- The difference and relation among list, set and map
- A common Dao class and util
- Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
- ZABBIX administrator forgot login password
猜你喜欢

Overall testing framework for performance testing

Not configured in app.json (uni releases wechat applet)

zabbix管理员忘记登录密码

How vscode debug nodejs

关于构建网络安全知识库方向相关知识的学习和思考

Learning and thinking about the relevant knowledge in the direction of building network security knowledge base

The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic

ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测
![Property datasource is required exception handling [idea]](/img/f3/50d36ed9445695c02e2bd622109d66.png)
Property datasource is required exception handling [idea]

Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
随机推荐
Similarities and differences between nor flash and NAND flash
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022
在哪家证券公司开户最好最安全 如何开户炒股票
DS graph - minimum spanning tree
Detailed explanation of IO model (easy to understand)
Summary of feature selection: filtered, wrapped, embedded
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
Performance test - Test Execution
Number of bytes occupied by variables of type char short int in memory
文件操作详解
DDD based on ABP -- Entity creation and update
Tiger mouth waterfall: Tongliang version of xiaohukou waterfall
spark:获取日志中每个时间段的访问量(入门级-简单实现)
DS diagram - the shortest path of the diagram (excluding the code framework)
DS binary tree - maximum distance of binary tree nodes
Must use destructuring props assignmenteslint
pip换源
The server switches between different CONDA environments and views various user processes
AtCoder Beginner Contest 261E // 按位思考 + dp
Which securities company is good at opening an account with flush? Excuse me, is it safe to open an account with mobile phone or stock?