Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
36 lines (31 loc) · 964 Bytes

File metadata and controls

36 lines (31 loc) · 964 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//c++11/14之前,Apple::b只是常量表达式,不分配内存,但是c++17/20以后,都是可以分配内存的,show(int)
//但是仍然不能把形参int&绑定到static constexpr类型,const限定符会丢失,show1(int &),可以绑定到show2(const int &)
#include <iostream>
class Apple
{
public:
static int a;
static constexpr int b = 20;
};
int Apple::a = 10;
//只是静态变量,必须要在声明之后定义(初始化),分配内存,否值链接会报错undefined referenece to Apple::a
//而且只能在main函数外,main内部不容许
// int Apple::b; //c++17之后,都不需要显示声明了
void show(int i)
{
std::cout << i << std::endl;
}
void show1(int &i)
{
std::cout << i << std::endl;
}
void show2(const int &i) //如果不加const,限制符会丢失(编译不过)
{
std::cout << i << std::endl;
}
int main()
{
std::cout << Apple::b << std::endl;
show(Apple::b);
show2(Apple::b);
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.