The pros and cons of Const and # define in C + +

Source: Internet
Author: User

The pros and cons of Const and # define, thus deriving the meaning of const;

Both const and # define have a similar function, which is to define a "constant";

This is the way you want to replace a # define constant. This is a way to define macros. Because macro substitution definition constants have some drawbacks: do not type check, no scope restrictions (this is easily followed by pollution).

#include <iostream>
#include <string>
using namespace Std;

void Myfunc1 () {
#define a 10
}

void Myfunc2 () {
printf ("a=%d\n", a);
}

int main () {
printf ("Print Outside: a=%d\n", a);
Myfunc1 ();
MYFUNC2 ();

system("pause");return 0;

}

Because only literal direct substitution, the global is valid, so no matter where the definition is, the global can be accessed. Because it is in the pre-compilation time to replace (as long as there is a definition, at the time of the pre-compilation of the entire replacement, so the outside can be accessed).

At the same time, it is easy to be contaminated.

#include <iostream>
#include <string>
using namespace Std;

#define a 10

void Myfunc1 () {
#define a 20
printf ("Myfunc1 Inside: a=%d\n", a);
}

void Myfunc2 () {
printf ("Myfunc2 Inside: a=%d\n", a);
}

int main () {
printf ("Print Outside: a=%d\n", a);
Myfunc1 ();
MYFUNC2 ();

system("pause");return 0;

}
Prompts for macro redefinition, and the results are all changed to new:

The way the macro is equivalent to global variables, whether in the function or outside the name of the function should be carefully carved (a bit of headache), otherwise it is easy to later in the new function is accidentally replaced, this is why use it to define constants are basically all uppercase, and the variables are lowercase, so since do not remember how many macro names, Nor conflict. But it is still not a global solution.

Because of the limitation of scope, const solves the problem of pollution global variable.

The following program is not possible:

#include <iostream>
#include <string>
using namespace Std;

void Myfunc1 () {
const int a = 20;
printf ("Myfunc1 Inside: a=%d\n", a);
}

void Myfunc2 () {
printf ("Myfunc2 Inside: a=%d\n", a);
}

int main () {
printf ("Print Outside: a=%d\n", a);
Myfunc1 ();
MYFUNC2 ();

system("pause");return 0;

}

Define a global read-only variable:

#include <iostream>
#include <string>
using namespace Std;

const int a = 10;
void Myfunc1 () {
const int a = 20;
printf ("Myfunc1 Inside: a=%d\n", a);
}

void Myfunc2 () {
printf ("Myfunc2 Inside: a=%d\n", a);
}

int main () {
printf ("Print Outside: a=%d\n", a);
Myfunc1 ();
MYFUNC2 ();

system("pause");return 0;

}

The inside of neither interference outside, but also can have priority points, at the same time to do the global can also do the global.

In such a new function to use the name of a, do not think about anything, just use it. Does not affect the previously defined global variables A, is not much more convenient ah.

Const is a read-only variable, essentially a variable, is a variable can pass parameters, and const also do type checking, so the benefits are more, such as: Do formal parameters, can receive different parameters, more flexible.

You can't change my variables in it, you can pass different variables, so you know more flexible;

#include <iostream>
#include <string>
using namespace Std;

void myfunc1 (const int k) {
printf ("Data =%d\n inside the Myfunc1", K);
}

int main () {
const int a = 20;
Myfunc1 (a);

const int b = 30;myfunc1(b);system("pause");return 0;

}

Application of const:

Because it is a read-only variable, the outside arguments are protected, and outside arguments are passed in and cannot be modified in the function body. So let the outside arguments be considered safe.

#include <iostream>
#include <string>
using namespace Std;

void myfunc1 (const int k) {
K = 3;
printf ("Data =%d\n inside the Myfunc1", K);
}

int main () {
const int a = 20;
MYFUNC1 (&a);

system("pause");return 0;

}


Macro substitution is the equivalent of making global variables, easily contaminated, without scope constraints, and cannot be differentiated by priority. It was replaced at the time of pre-compilation.

While the const is in the compile time only assigns the variable, has the scope distinction, and the type consistent security detection, applies the const to develop the project to be more convenient and flexible ...

Macro substitution defines constants, which must be globally valid;

Const defines a read-only variable that has a scope, can do a global, can also do local, there are priority points. It is both convenient and safe to replace # define. Why does it all exist? Because they are also good, just want to take their own advantages:

The way the macro is replaced makes the whole compilation process slow (precompiled time + true compile time), but makes the program run faster, because it has been directly replaced (macro expansion), run directly.

Const and it on the contrary, the entire compilation time is small, but the program runs slowly, because to find the memory space to open the variable ...

The pros and cons of Const and # define in C + +

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.