C++const Qualifier

Source: Internet
Author: User

In the C language we work with symbolic constants in the way defined by the # define macro. In C + + there is a better way to handle symbolic constants, which is to use the Const keyword to modify the declaration and initialization of variables. The benefit of this method of handling constants is self-evident: if the program needs to use the same value in multiple places, we might as well define the value as a constant, so that when you need to modify the value, you just have to modify the constant to save a lot of trouble.

In addition to the macro definition of C language, the advantage of the Const qualifier is: 1, the ability to explicitly specify the type, 2, you can use the scope of C + + to define the definition of a specific function or file. The following code:

#include <iostream>
#include <string>
#define Mon ' C '
#define MOS 1.5
#define MOM "ch"
#define MAX 100000000
int main ()
{
Using Std::cout;
Using Std::endl;
Using Std::string;

const int Month = 12;
const char char = ' C ';
string Str = "Hello";
cout<<month<<endl;
cout<<str<<endl;
cout<<char<<endl;
cout<<mon<<endl<<max<<endl;

return 0;
}

C++const Qualifier

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.