zoukankan      html  css  js  c++  java
  • C/C++ Basics>about #define, const

    1.
        In C programming, the normal use of #define is to declare an constant vairiable. But it's been replaced by const.
    C: #define X 100   ----> C++: const int x=100;

    2.
        #define can also define Macro with parameters. But its functionality can also be replaced by inline function with C++
    C: #define MAX(a,b) ((a)>(b)?(a):(b))
    C++: inline int max(int a,int b){ return a>b?a:b;}

    3.
        #define is most used in conditional compling. the conditional compling instruction includes:#if,#else,#endif,#ifdef,#ifndef,#undef.
        it always be used to avoid the head file being referenced more than once.
        eg.
        #ifndef NULL
            #define NULL ((void*)0)
        #endif

        #ifndef _myheadfile_h
            #define _myheadfile_h
        #endif

     

    4.const

    const有很多作用,主要可以有以下几种:

    1. const int x=10; //定义一个常量 ,该常量分配在静态存储区

    2 const int *p;//p是一个常量pointer,只能指向常量,因此不能进行左操作

    3 int f(const int x) //表示f函数中不能修改x的值

    4 int f(int x) const //这种主要用在类的成员函数的修饰上,表示该函数不会修改类的成员变量。
        

  • 相关阅读:
    HDU 2112 HDU Today
    HDU 1869 六度分离
    HDU 3790 最短路径问题
    HDU2066 一个人的旅行
    HDU1596 find the safest road(最短路)
    HDU 1254 推箱子(双重bfs)
    HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
    HDU 1045 Fire Net
    数据结构之单链表头插法,尾插法
    Java--会移动、反弹的球
  • 原文地址:https://www.cnblogs.com/Winston/p/1142289.html
Copyright © 2011-2022 走看看