当前位置:网站首页>Colon function and explicit keyword in constructor
Colon function and explicit keyword in constructor
2022-04-23 21:57:00 【Three stone orders】
The colon in the constructor functions , Class assigns values to member variables , Constants that are more suitable for member variables const type .
class myClass
{
/* The colon in the constructor functions , Class assigns values to member variables , Constants that are more suitable for member variables const type .*/
public :
myClass();// Constructors , No return type , You can have a list of parameters , There is no need for
~myClass();// Destructor
int a;
const int b;
}
myClass::myClass():a(1),b(1)// Initialization list
{
}
myClass::myClass()
{
a = 1;// you 're right , The effect is equivalent to initializing in the initialization list
b = 1;// error ,const Variables cannot be assigned ;
}
Constructor with explicit keyword , Disable implicit conversion , That is, when calling this class, you must declare that it is a class ; Without this keyword, you can directly assign the data type value in the constructor
class AAA
{
public:
//AAA(int a) : num(a) {}
explicit AAA(int a) : num(a) {
}
int GetValue() {
return num; }
private:
int num;
};
int fun(AAA aaa)
{
return (int)aaa.GetValue() + 1;
}
int main()
{
//int a = fun(5);
int a = fun(AAA(5));
}
版权声明
本文为[Three stone orders]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200615437072.html
边栏推荐
- Use 3080ti to run tensorflow GPU = 1 X version of the source code
- Mixed use of Oracle column row conversion and comma truncated string
- Centralized record of experimental problems
- [leetcode refers to offer 27. Image of binary tree (simple)]
- Tear the Google SRE book by hand
- This paper solves the cross domain problem of browser
- Tsinghua University | webface260m: benchmark for million level deep face recognition (tpami2022)
- JS merge duplicate data in array object
- Presto on spark supports 3.1.3 records
- Deep understanding of modern mobile GPU (continuously updating)
猜你喜欢
C language programming based on loop structure (PTA)
Introduction to tensorrt
Question brushing plan - depth first search DFS (I)
DW basic tutorial (I)
Is rust more suitable for less experienced programmers?
[leetcode refers to offer 52. The first common node of two linked lists (simple)]
Minecraft 1.12.2 module development (43) custom shield
What if Jenkins forgot his password
Preliminary analysis of Airbase
Database experiment VI integrity language experiment
随机推荐
Alibaba cloud responded to the disclosure of user registration information
Deno 1.13.2 发布
A series of problems of C DataGridView binding list
Database Experiment 7 stored procedure experiment
presto on spark 支持3.1.3记录
Tsinghua University | webface260m: benchmark for million level deep face recognition (tpami2022)
C winfrom DataGridView click on the column header can not automatically sort the problem
Strictly, severely and quickly strengthen food safety supervision during the epidemic in Shanghai
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
ubutnu20安装CenterNet
Thread safe sigleton (singleton mode)
引入结构化并发,Swift 5.5 发布!
1. Finishing huazi Mianjing -- 1
从严从重从快 上海全面加强疫情期间食品安全监管
Resolve the "chromedriver executable needs to be in path" error
Pycharm Chinese plug-in
Plato Farm元宇宙IEO上线四大,链上交易颇高
JS merge duplicate data in array object
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high