Using compiler constants in C + + to replace preprocessing constants

Source: Internet
Author: User
Tags definition constant constant definition

For simple constants that are preprocessed, the const type can be used instead;

In object-oriented programming, constants within a class can be replaced with static const members.

Note within the class (In-class), the static const member only allows an integer constant to be assigned, if the other type is declared within the class, and the method is defined outside the class;

You can also use the "enum hack" to provide a const effect and give a built-in (built-in) array declaration;

There are many problems in preprocessing function call, which can be replaced by template inline (template inline), and it can achieve high efficiency.

See code specifically, and comments;

Code (/*eclipse CDT; GCC 4.7.1*/):

* * * effectivecpp.cpp * * Created on:2013.11.13 * author:caroline//*eclipse CDT; GCC 4.7.1*/#include <iostream> #include <string> #include <array> #include <algo  
      
Rithm> using namespace std; #define Aspect_ratio 1.653//aspect ratio/* More Highlights: http://www.bianceng.cnhttp://www.bianceng.cn/programming/cplus/*/#define Call_with_max (A, B) f ((a) > (b)?  
    (a): (b)) class Gameplayer {public:static const int numturns = 5; static const double PI; Cannot initialize non-integral enum {enumturns = 5} in class;  
    Enumeration type Std::array<int, numturns> scores;  
Std::array<int, enumturns> escores;  
};  
      
Const DOUBLE Gameplayer::P i = 3.14;  
Template<typename t> void F (T a) {std::cout << "F:" << A; } template<typename t> inline void Callwithmax (const t& A, const t& B) {f (a>b? a:b  
);  
   int main (void) {   
    Preprocessor definition std::cout << "aspect_ratio =" << aspect_ratio << Std::endl;  
    Constant Definition const Double aspectratio = 1.653;  
      
    Std::cout << "aspectratio =" << aspectratio << Std::endl;  
    Constant Pointer const char* CONST AUTHORNAME = "Caroline";  
      
    Std::cout << "AuthorName =" << authorname << Std::endl;  
    Constant Pointer const std::string sauthorname ("Caroline");  
      
    Std::cout << "sauthorname =" << sauthorname << Std::endl;  
    Class exclusive constant Gameplayer GP;  
    Std::array<int, gameplayer::numturns> scores = {1, 2, 3, 4, 5}};  
    Gp.scores = scores;  
    Std::cout << "Gp.scores:";  
    for (const auto s:gp.scores) std::cout << s << "";  
    Std::cout << Std::endl;  
      
    Std::cout << "Gameplayer::P i =" << gameplayer::P i << Std::endl;  
    TEST macro int a = 5, b = 0; Call_With_max (++a, b); A, + + two times std::cout << ";  
    A = "<< a <<std::endl;  
    A = 5, b = 0; Call_with_max (++a, b+10); A, + + once std::cout << ";  
      
    A = "<< a <<std::endl;  
    Template inline A = 5, b = 0;  
    Callwithmax (++a, b); Std::cout << ";  
      
    A = "<< a <<std::endl;  
      
return 0; }

Output:

Aspect_ratio = 1.653  
aspectratio = 1.653  
authorname = Caroline sauthorname  
= Caroline gp.scores:1  
2 3 4 5   
gameplayer::P i = 3.14  
f:7; a = 7  
f:10; a = 6  
f:6; a = 6

Author: csdn Blog spike_king

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.