c+const_如何在C ++中使用const? 初学者指南

c+const

Hey, folks! In this tutorial, we will be focusing on const in C++.

嘿伙计! 在本教程中,我们将重点介绍C ++中的const



C ++中const的基础 (Basics of const in C++)

C++ has a huge set of keywords that help us give special functionality to the data variables on a whole.

C ++有大量的关键字,可以帮助我们整体上为数据变量提供特殊功能。

const in C++ makes a particular data entity, a constant one i.e. during the scope of the entire program, the value of the const variable remains the same.

C ++中的const使特定的数据实体成为一个常数,即在整个程序范围内,const变量的值保持不变。

In the actual scenarios, the const keyword is very essential to lock a particular value as static and constant throughout its use.

在实际方案中,const关键字对于在使用过程中将特定值锁定为静态和常量非常重要。

Let us take an example.

让我们举个例子。

For an automatic emailing system, wherein the owner of a company wants to send a mail at the last date of every month to his employees, the email address of the owner would stay the same. So we can declare a variable to save the owner’s email address and make it constant using the const keyword.

对于自动电子邮件系统,其中公司的所有者希望在每个月的最后一天向其员工发送邮件,所有者的电子邮件地址将保持不变。 因此,我们可以声明一个变量来保存所有者的电子邮件地址,并使用const关键字使其保持不变。

Thus, we would actually save time and reduce the complexity of the system.

因此,我们实际上将节省时间并降低系统的复杂性。



C ++中const的变体 (Variants of const in C++)

The const keyword in C++ can be used alongside different data entities of programming such as:

C ++中的const关键字可以与编程的不同数据实体一起使用,例如:

  • Data variables

    资料变数
  • Function arguments

    函数参数
  • Pointers

    指针
  • Objects of a class

    类的对象
  • Class data members

    类数据成员


1.数据变量为const (1. Data variables as const)

When a data variable is declared as const, the value equated to the variable is treated as constant and is immutable.

当数据变量声明为const时,等于该变量的值被视为常量并且是不可变的

Syntax:

句法:


const data-type variable = value;

Example:

例:


#include<iostream>
using namespace std;
int main()
{
    const int A=10,B=20;
    cout<<"Addition: "<<(A+B);
  
}

Output:

输出:


Addition: 30

Error and Exceptions:

错误和异常:

If we try to change or manipulate the value of a const data variable, the compiler would throw an error saying that we cannot assign values to a read-only variable.

如果尝试更改或操纵const数据变量的值,则编译器将引发错误,表明我们无法将值分配给只读变量。


#include<iostream>
using namespace std;
int main()
{
    const int A=10;
    A+=10;
    cout<<A;
  
}

Output:

输出:


main.cpp: In function ‘int main()’:
main.cpp:6:8: error: assignment of read-only variable ‘A’
     A+=10;


2.函数参数在C ++中为const (2. Function arguments as const in C++)

We can even assign an argument to a function/method as constant, thus restricting all the changes to the function argument.

我们甚至可以将一个参数分配给函数/方法作为常量,从而将所有更改限制在该函数参数上。

Syntax:

句法:


data-type function(const data-type variable)
{
  //body
}

Example:

例:


#include<iostream>
using namespace std;

int add(const int x)
{   
    int res=0;
    int error=20;
    res = x+error;
    return res;
}
int main()
{
  int ans = add(50);
  cout<<"Addition:"<<ans;
}

Output:

输出:


Addition:70

Error and Exceptions:

错误和异常:

As seen below, if we try to manipulate the value of a const function argument, an error would be raised by the compiler.

如下所示,如果我们尝试操纵const函数参数的值,则编译器将引发错误。


#include<iostream>
using namespace std;

int add(const int x)
{   x=x+100;
    return x;
}
int main()
{
  int ans = add(50);
  cout<<"Addition:"<<ans;
}

In the above example, we are trying to change the value of the const function argument “x”, resulting in a compiler error.

在上面的示例中,我们试图更改const函数参数“ x”的值,从而导致编译器错误。

Output:

输出:


main.cpp: In function ‘int add(int)’:
main.cpp:5:9: error: assignment of read-only parameter ‘x’
 {   x=x+100;


3.指针为const (3. Pointer as const)

Pointer variables can be declared as constant too using the below syntax:

指针变量也可以使用以下语法声明为常量:


data-type const *variabl;

Example:

范例


#include<iostream>
using namespace std;
int main()
{   int age = 21;
    int const *point = & age;
    cout<<"Pointer value: "<<*point;
  
}

Output:

输出:


Pointer value: 21

Error and Exceptions:

错误和异常:

If we try to manipulate the value of a const pointer, that leads to a compilation error.

如果我们尝试操纵const指针的值,则会导致编译错误。


#include<iostream>
using namespace std;
int main()
{   int age = 21;
    int const *point = & age;
    *point=*point+10;
    cout<<"Pointer value: "<<*point;
  
}

Output:

输出:


main.cpp: In function ‘int main()’:
main.cpp:6:19: error: assignment of read-only location ‘* point’
     *point=*point+10;
                   


4.类的数据变量为const (4. Data variables of a class as const)

The Class variables can also be treated as const to make the value of the data variable as constant and immutable.

也可以将Class变量视为const,以使数据变量的值恒定且不变。

Syntax:

句法:


const data-type variable = value;

Example:

例:


#include<iostream>
using namespace std;
class Demo
{
    public:
    const int X = 10;
    
};
int main()
{
   Demo d;
   cout<<d.X;
  
}

Output:

输出:


10

Error and Exceptions:

错误和异常:


#include<iostream>
using namespace std;
class Demo
{
    public:
   const int X = 10;
    
};
int main()
{
   Demo d;
   d.X=100;
   cout<<d.X;
  
}

Output:

输出:


main.cpp: In function ‘int main()’:
main.cpp:12:8: error: assignment of read-only member ‘Demo::X’
    d.X=100;


5.类的对象为const (5. Object of a class as const)

Even the objects of a class can work with the const keyword. As soon as we declare the object of a class as constant, the data variables of the same becomes immutable i.e. the values of the data variables that belongs to the class pointed by the object cannot be changed.

甚至类的对象也可以使用const关键字。 一旦我们将一个类的对象声明为常量,该类的数据变量就变得不可变,,属于该对象所指向的类的数据变量的值无法更改

Syntax:

句法:


const Class object;

Example:

例:


#include<iostream>
using namespace std;
class Demo
{
    public:
    int X = 10;

};
int main()
{
   const Demo d;
   cout<<d.X;
  
}

Output:

输出:


10

Error and Exceptions:

错误和异常


#include<iostream>
using namespace std;
class Demo
{
    public:
    int X = 10;

};
int main()
{
   const Demo d;
   d.X=100;
   cout<<d.X;
  
}

Output:

输出:


main.cpp: In function ‘int main()’:
main.cpp:12:8: error: assignment of member ‘Demo::X’ in read-only object
    d.X=100;


结论 (Conclusion)

Thus, in this article, we have unleashed the working of const keyword alongside data variables, pointers, Class objects, etc.

因此,在本文中,我们释放了const关键字以及数据变量,指针,Class对象等的工作。



参考资料 (References)

C++ const keyword — Documentation

C ++ const关键字—文档

翻译自: https://www.journaldev.com/39812/const-in-c-plus-plus

c+const

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值