代码之家  ›  专栏  ›  技术社区  ›  Etan bbum

“const&arg”与“t arg”

c++
  •  15
  • Etan bbum  · 技术社区  · 16 年前

    下面哪个示例是声明以下函数的更好方法,为什么?

    void myFunction (const int &myArgument);
    

    void myFunction (int myArgument);
    
    14 回复  |  直到 7 年前
        1
  •  36
  •   Alexey Malistov    16 年前

    const T & arg sizeof(T)>sizeof(void*) T arg sizeof(T) <= sizeof(void*)

        2
  •  19
  •   Mehrdad Afshari    16 年前

    const T& T arg private

    class Demo {
        public: Demo() {} 
        private: Demo(const Demo& t) { } 
    };
    
    void foo(Demo t) { }
    
    int main() {
        Demo t;
        foo(t); // error: cannot copy `t`.
        return 0;
    }
    

        3
  •  12
  •   Johannes Schaub - litb    16 年前

    Move constructors

    1. void Transmogrify(Widget& toChange);
      void Increment(int* pToBump);
      
    2. double Cube(double value);
      

    sizeof(T)

        5
  •  5
  •   Jerry Coffin    16 年前
        6
  •  3
  •   Leandro T. C. Melo    16 年前

        7
  •  2
  •   Sean    16 年前

        9
  •  1
  •   Brad    16 年前

        10
  •  1
  •   Yacoby    16 年前

        11
  •  0
  •   Community CDub    8 年前
        12
  •  0
  •   David Thornley    16 年前

    const const_cast<>

    auto_ptr<>

        13
  •  -1
  •   JSBÕ±Õ¸Õ£Õ¹    16 年前

        14
  •  -1
  •   Tom    16 年前