博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言 关键字const_C ++ const关键字| 查找输出程序| 套装1
阅读量:2527 次
发布时间:2019-05-11

本文共 2508 字,大约阅读时间需要 8 分钟。

c语言 关键字const

Program 1:

程序1:

#include 
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 . Without a const keyword the above code will work fine.

上面的代码将产生错误,因为我们无法将非成员函数定义为 。 如果没有const关键字,则上面的代码可以正常工作。

Program 2:

程式2:

#include 
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 
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 
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 class Sample that contains two const data members A and B. As we know that we can assign the values of constant at the time of declaration. But here we use the concept of member initialize list.

在这里,我们创建了一个Sample类,其中包含两个const数据成员AB。 众所周知,我们可以在声明时分配常量的值。 但是这里我们使用成员初始化列表的概念。

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

转载地址:http://wbtzd.baihongyu.com/

你可能感兴趣的文章
Python -- machine learning, neural network -- PyBrain 机器学习 神经网络
查看>>
一道dp题目
查看>>
mysql中间件研究(tddl atlas cobar sharding-jdbc)
查看>>
Cast-128 加密算法和 MyPassWord 的破解
查看>>
4.28下午 听力611
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
《机器学习实战》学习笔记第二章 —— K-近邻算法
查看>>
uni-app 引入本地iconfont的正确姿势以及阿里图标引入
查看>>
DSB
查看>>
Java中的阻塞队列
查看>>
前端软件sublime的一些常用快捷键
查看>>
openssl 升级
查看>>
2017.10.30 天晴 昨天十公里没减肥
查看>>
Git 打标签(分布式版本控制系统)
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>