当前位置:网站首页>Forward declaration of classes
Forward declaration of classes
2022-06-30 14:50:00 【_ dhx_】
Forward declaration of class
Although using forward reference declarations can solve some problems , But it's not everything . It should be noted that , Despite the use of forward reference declarations , But before providing a complete class declaration , Cannot define objects of this class , You cannot use objects of this class in inline member functions .
#include<bits/stdc++.h>
using namespace std;
class A;
// The statement here can only state A Is a class name
// Instead of giving a complete definition of this class , Therefore, it cannot be accessed A Internal members of
class B
{
int m_B=0;
A *Point_A; // The definition type is A* The pointer to
void Add()
{
m_B+=Point_A->m_A;
}
};
class A
{
public:
int m_A=10;
};
int main()
{
}
At this time, the run will error(field ‘member_A’ has incomplete type ‘A’: Field “member_A” The type of “A” Incomplete ).
resolvent
1. Will need to be called to class A Functions defined outside the class , And define to class A Behind
#include<bits/stdc++.h>
using namespace std;
class A; // The statement here can only state A Is a class , Instead of accessing A Internal members of
class B
{
int m_B;
A *Point_A; // The definition type is A* The pointer to
void Add();
};
class A
{
public:
int m_A=10;
};
void B::Add() // Definition Add function
{
m_B+=Point_A->m_A;
}
int main()
{
}
2. Direct will class A Define the class B Before
#include<bits/stdc++.h>
using namespace std;
class A
{
public:
int m_A=10;
};
class B
{
int m_B=0;
A *Point_A;
void Add()
{
m_B+=Point_A->m_A;
}
};
int main()
{
}
summary :
C++ Class can be declared forward . however , Classes that are only forward declared but not defined are incomplete , Such a class , Can only be used to define pointers 、 quote 、 And pointers and references for function parameters , Objects cannot be defined ( Because the compiler only knows that this is a class , I don't know how big this class is ), And you can't access class objects in any form ( Because the compiler doesn't know class A What kind of members does it contain ).
边栏推荐
- C language & the difference between the address pointed to and the address pointed to by the pointer
- After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
- Distributed -- openresty+lua+redis
- @PathVariable
- DR-TANet: Dynamic Receptive Temporal Attention Network for Street Scene Change Detection
- [extensive reading of papers] attributes guided facial image completion
- JS array
- Uniapp upload image method
- CCF Z-scan (full mark code + problem solving ideas) 201412-2
- catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
猜你喜欢

2021-05-12
![【BUUCTF】[GXYCTF2019]Ping Ping Ping1](/img/dc/4d87dfb0c2fa9cd75b54e092fd3971.jpg)
【BUUCTF】[GXYCTF2019]Ping Ping Ping1

Ctfshow getting started with the web (ThinkPHP topic)

CCF date calculation (Full Score code + skill summary) February 2, 2015

Detailed explanation of settimeout() and setinterval()

How to realize selective screen recording for EV screen recording

Error $(...) size is not a function

Learn about data kinship JSON format design from sqlflow JSON format

PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available

DiceCTF - knock-knock
随机推荐
Upgrade composer self update
CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3
Binary rotation array (2)
PHP recursive multi-level classification, infinite classification
Larave8 JWT API expiration custom error return
V3 02——What‘s new in Chrome extensions
Solution cannot use a scalar value as an array
Uniapp upload image method
Svn password forgetting solution
An error is reported when installing dataspherestudio doc: invalid default value for 'update_ time‘
立式加工中心的数控加工对刀具使用基本要求
Programming of left-hand trapezoidal thread
Basic requirements for tool use in NC machining of vertical machining center
【BUUCTF】 Have Fun
數據恢複軟件EasyRecovery15下載
Use of laravel repository mode
Notepad regular delete the line of the keyword
Distributed -- openresty+lua+redis
Judgment of deep learning experiment results
In situ merging of two arrays with two pointers