c语言 关键字const
Program 1:
程序1:
#include <iostream>using namespace std;void fun(int& A) const{A = 10;}int main(){int X = 0;fun(X);cout << X;return 0;}
Output:
输出:
[Error] non-member function 'void fun(int)' cannot have const qualifier.
Explanation:
说明:
The above code will generate an error because we cannot define, the non-member function as a const. Without a const keyword the above code will work fine.
上面的代码将产生错误,因为我们无法将非成员函数定义为const 。 如果没有const关键字,则上面的代码可以正常工作。
Program 2:
程式2:
#include <iostream>using namespace std;int main(){const int X = 0;int* ptr;ptr = &X;*ptr = 10;cout << X;return 0;}
Output:
输出:
error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive] ptr = &X;
Explanation:
说明:
The above program will generate an error in C++, C++ does not allow modification in a constant using pointer, but we can modify the value of a constant in C language. The below program in C language will work fine.
上面的程序在C ++中会产生错误,C ++不允许使用指针修改常量,但是我们可以在C语言中修改常量的值。 下面的C语言程序可以正常运行。
#include <stdio.h>int main(){const int X = 0;int* ptr;ptr = &X;*ptr = 10;printf("%d", X);return 0;}
Program may run on C language compiler, but it is not a standard that we can change the constant. In C language compiler – it can be changed through the pointer.
程序可以在C语言编译器上运行,但是我们不能更改常量不是标准。 在C语言编译器中–可以通过指针进行更改。
Program 3:
程式3:
#include <iostream>using namespace std;class Sample {const int A;const int B;public:Sample(): A(10), B(20){}void print(){cout << A << " " << B;}};int main(){Sample S;S.print();return 0;}
Output:
输出:
10 20
Explanation:
说明:
The above code will print"10 20"on the console screen.
上面的代码将在控制台屏幕上显示“ 10 20”。
Let's understand the program step by step.
让我们逐步了解程序。
Here we created a classSamplethat contains two const data membersAandB. As we know that we can assign the values of constant at the time of declaration. But here we use the concept ofmember initialize list.
在这里,我们创建了一个Sample类,其中包含两个const数据成员A和B。众所周知,我们可以在声明时分配常量的值。 但是这里我们使用成员初始化列表的概念。
Sample ():A(10),B(20){}
We can assign value to const data members using the member initialize list. We can initialize members by a colon (:) with members and value in the constructor.
我们可以使用成员初始化列表为const数据成员分配值。 我们可以初始化一个冒号成员:在构造函数中成员和值()。
Here we also defined a print() member function, which is used to print values of data members.
在这里,我们还定义了一个print()成员函数,该函数用于打印数据成员的值。
Recommended posts
推荐的帖子
C++ const Keyword | Find output programs | Set 2
C ++ const关键字| 查找输出程序| 套装2
C++ Operators | Find output programs | Set 1
C ++运算符| 查找输出程序| 套装1
C++ Operators | Find output programs | Set 2
C ++运算符| 查找输出程序| 套装2
C++ Reference Variable| Find output programs | Set 1
C ++参考变量| 查找输出程序| 套装1
C++ Reference Variable| Find output programs | Set 2
C ++参考变量| 查找输出程序| 套装2
C++ Conditional Statements | Find output programs | Set 1
C ++条件语句| 查找输出程序| 套装1
C++ Conditional Statements | Find output programs | Set 2
C ++条件语句| 查找输出程序| 套装2
C++ Switch Statement | Find output programs | Set 1
C ++转换语句| 查找输出程序| 套装1
C++ Switch Statement | Find output programs | Set 2
C ++转换语句| 查找输出程序| 套装2
C++ goto Statement | Find output programs | Set 1
C ++ goto语句| 查找输出程序| 套装1
C++ goto Statement | Find output programs | Set 2
C ++ goto语句| 查找输出程序| 套装2
C++ Looping | Find output programs | Set 1
C ++循环| 查找输出程序| 套装1
C++ Looping | Find output programs | Set 2
C ++循环| 查找输出程序| 套装2
C++ Looping | Find output programs | Set 3
C ++循环| 查找输出程序| 套装3
C++ Looping | Find output programs | Set 4
C ++循环| 查找输出程序| 套装4
C++ Looping | Find output programs | Set 5
C ++循环| 查找输出程序| 套装5
翻译自: /cpp-tutorial/const-keyword-find-output-programs-set-1.aspx
c语言 关键字const
如果觉得《c语言 关键字const_C ++ const关键字| 查找输出程序| 套装1》对你有帮助,请点赞、收藏,并留下你的观点哦!