c語言 關鍵字const_C ++ const關鍵字| 查找輸出程序| 套裝1

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 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 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

翻譯自: https://www.includehelp.com/cpp-tutorial/const-keyword-find-output-programs-set-1.aspx

c語言 關鍵字const

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541918.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541918.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541918.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【喜報】JEEWX榮獲“2016 年度碼云新增熱門開源軟件排行榜”第一名!

為什么80%的碼農都做不了架構師&#xff1f;>>> 2016 年度碼云新增項目排行榜 TOP 50 正式出爐&#xff01;根據 2016 年在碼云上新增開源項目的 Watch、Star、Fork 數量以及其他角度的統計&#xff0c;JEEWX捷微管家榮獲“2016 年度碼云新增熱門開源軟件排行榜”第…

java 二叉樹特點_瘋狂java筆記之樹和二叉樹

樹的概述樹是一種非常常用的數據結構&#xff0c;樹與前面介紹的線性表&#xff0c;棧&#xff0c;隊列等線性結構不同&#xff0c;樹是一種非線性結構1.樹的定義和基本術語計算機世界里的樹&#xff0c;是從自然界中實際的樹抽象而來的&#xff0c;它指的是N個有父子關系的節點…

編輯距離 dp_使用動態編程(DP)編輯距離

編輯距離 dpProblem: You are given two strings s1 and s2 of length M and N respectively. You can perform following operations on the string. 問題&#xff1a;給您兩個長度分別為M和N的字符串s1和s2 。 您可以對字符串執行以下操作。 Insert a character at any posi…

tomcat +apache 配置集群

2019獨角獸企業重金招聘Python工程師標準>>> APACHE2.2.25TOMCAT6.0.37配置負載均衡 目標: 使用 apache 和 tomcat 配置一個可以應用的 web 網站&#xff0c;要達到以下要求&#xff1a; 1. Apache 做為 HttpServer &#xff0c;后面連接多個 tomcat 應用實例&…

java雙緩存機制_詳解JVM類加載機制及類緩存問題的處理方法

前言大家應該都知道&#xff0c;當一個Java項目啟動的時候&#xff0c;JVM會找到main方法&#xff0c;根據對象之間的調用來對class文件和所引用的jar包中的class文件進行加載(其步驟分為加載、驗證、準備、解析、初始化、使用和卸載)&#xff0c;方法區中開辟內存來存儲類的運…

oracle中dbms_并發和由于DBMS中的并發導致的問題

oracle中dbms并發 (Concurrency) The ability of a database system which handles simultaneously or a number of transactions by interleaving parts of the actions or the overlapping this is called concurrency of the system. 數據庫系統通過交織部分操作或重疊操作來…

什么是mvc?

什么是MVCMVC 是一種設計模式&#xff0c;它將應用劃分為3 個部分&#xff1a;數據&#xff08;模型&#xff09;、展現層&#xff08;視圖&#xff09;和用戶交互層&#xff08;控制器&#xff09;。換句話說&#xff0c;一個事件的發生是這樣的過程&#xff1a;1&#xff0e;…

mysql的安裝和基本命令_MySQL安裝以及簡單命令用法

MYSQL:關系型數據庫存儲引擎:負責將邏輯層的概念轉化為物理層機制&#xff0c;在物理層完成物理機制。支持事務&#xff1a;transaction必須滿足的條件&#xff1a;ACID(一致性,持久性,原子性,隔離性)鎖&#xff1a;并發訪問隨機訪問&#xff1a;數據在磁盤上是隨機存儲的安裝&…

將數組轉換為JavaScript中的對象

Lets say you have the following array, 假設您有以下數組&#xff0c; const nums [1, 2, 3, 4, 5];console.log(nums);Output 輸出量 (5) [1, 2, 3, 4, 5]We know that nums is an array and we can see in the output that we get an array. Lets convert it into an ob…

docker集群運行在calico網絡上

2019獨角獸企業重金招聘Python工程師標準>>> ##網絡及版本信息 docker1 centos7 192.168.75.200 docker2 centos7 192.168.75.201 物理網絡 192.168.75.1/24 Docker version 1.10.3, build 3999ccb-unsupported &#xff0c;安裝過程略 # calicoctl version Version…

python批量雷達圖_python批量制作雷達圖

老板要畫雷達圖&#xff0c;但是數據好多組怎么辦&#xff1f;不能一個一個點excel去畫吧&#xff0c;那么可以利用python進行批量制作&#xff0c;得到樣式如下&#xff1a;首先制作一個演示的excel&#xff0c;評分為excel隨機數生成&#xff1a;1 INT((RAND()4)*10)/10加入標…

JavaScript中帶有示例的Math.log()方法

JavaScript | Math.log()方法 (JavaScript | Math.log() Method) Math.log() is a function in math library of JavaScript that is used to return the value of natural Log i.e. (base e) of the given number. It is also known as ln(x) in mathematical terms. Math.log…

SUI踩坑記錄

SUI踩坑記錄 最近做了個項目選型了SUI和vue做單頁應用。下面記錄一下踩坑經歷SUI 介紹 sui文檔&#xff1a;http://m.sui.taobao.org/SUI Mobile 是一套基于 Framework7 開發的UI庫。它非常輕量、精美&#xff0c;只需要引入我們的CDN文件就可以使用&#xff0c;并且能兼容到 i…

java 寫入xml文件_java讀寫xml文件

要讀的xml文件李華姓名>14年齡>學生>張三姓名>16年齡>學生>學生花名冊>package xml;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.Iterator;import java.util.Vector;import javax.xml.pa…

JavaScript中帶有示例的Math.max()方法

JavaScript | Math.max()方法 (JavaScript | Math.max() Method) Math.max() is a function in math library of JavaScript that is used to return the greatest value of all the passed values to the method. Math.max()是JavaScript數學庫中的函數&#xff0c;用于將所有…

java 修飾符默認_Java和C#默認訪問修飾符

C#中&#xff1a;針對下面幾種類型內部成員的訪問修飾符&#xff1a;enum的默認訪問修飾符&#xff1a;public。class的默認為private。interface默認為public。struct默認為private。其中&#xff1a;public可以被任意存取&#xff1b;protected只可以被本類和其繼承子類存取&…

JavaScript中帶有示例的Math.abs()方法

JavaScript | Math.abs()方法 (JavaScript | Math.abs() Method) Math operations in JavaScript are handled using functions of math library in JavaScript. In this tutorial on Math.abs() method, we will learn about the abs() method and its working with examples.…

人臉識別python face_recognize_python2.7使用face_recognition做人臉識別

偶然看到一篇文章&#xff0c;說是可以實時人臉識別&#xff0c;很有興趣就自己按照文章開始動手人臉識別&#xff0c;但是實現過程中遇到了幾個問題這里做個總結&#xff0c;希望可以幫助到大家安裝face_recognition這個之前需要先安裝編譯dlib&#xff0c;如果沒有安裝dlib&a…

c# reverse_清單 .Reverse()方法,以C#為例

c# reverseC&#xff03;List <T> .Reverse()方法 (C# List<T>.Reverse() Method) List<T>.Reverse() method is used to reverse the all list elements. List <T> .Reverse()方法用于反轉所有列表元素。 Syntax: 句法&#xff1a; void List<T&…