<legend id='55aa2'><style id='55aa2'><dir id='55aa2'><q id='55aa2'></q></dir></style></legend>

<small id='55aa2'></small><noframes id='55aa2'>

    <bdo id='55aa2'></bdo><ul id='55aa2'></ul>
  • <i id='55aa2'><tr id='55aa2'><dt id='55aa2'><q id='55aa2'><span id='55aa2'><b id='55aa2'><form id='55aa2'><ins id='55aa2'></ins><ul id='55aa2'></ul><sub id='55aa2'></sub></form><legend id='55aa2'></legend><bdo id='55aa2'><pre id='55aa2'><center id='55aa2'></center></pre></bdo></b><th id='55aa2'></th></span></q></dt></tr></i><div id='55aa2'><tfoot id='55aa2'></tfoot><dl id='55aa2'><fieldset id='55aa2'></fieldset></dl></div>

        <tfoot id='55aa2'></tfoot>
      1. '&' 是什么意思在 C++ 声明中做什么?

        What does #39;amp;#39; do in a C++ declaration?( 是什么意思在 C++ 声明中做什么?)

          <bdo id='bJix2'></bdo><ul id='bJix2'></ul>

          • <small id='bJix2'></small><noframes id='bJix2'>

            <legend id='bJix2'><style id='bJix2'><dir id='bJix2'><q id='bJix2'></q></dir></style></legend>
              <tbody id='bJix2'></tbody>
          • <i id='bJix2'><tr id='bJix2'><dt id='bJix2'><q id='bJix2'><span id='bJix2'><b id='bJix2'><form id='bJix2'><ins id='bJix2'></ins><ul id='bJix2'></ul><sub id='bJix2'></sub></form><legend id='bJix2'></legend><bdo id='bJix2'><pre id='bJix2'><center id='bJix2'></center></pre></bdo></b><th id='bJix2'></th></span></q></dt></tr></i><div id='bJix2'><tfoot id='bJix2'></tfoot><dl id='bJix2'><fieldset id='bJix2'></fieldset></dl></div>
            <tfoot id='bJix2'></tfoot>

                  本文介绍了'&' 是什么意思在 C++ 声明中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是一个 C 人,我正在尝试理解一些 C++ 代码.我有以下函数声明:

                  I am a C guy and I'm trying to understand some C++ code. I have the following function declaration:

                  int foo(const string &myname) {
                    cout << "called foo for: " << myname << endl;
                    return 0;
                  }
                  

                  函数签名与等效的 C 有何不同:

                  How does the function signature differ from the equivalent C:

                  int foo(const char *myname)
                  

                  使用 string *mynamestring &myname 有区别吗?C++ 中的 & 和 C 中的 * 表示指针的区别是什么?

                  Is there a difference between using string *myname vs string &myname? What is the difference between & in C++ and * in C to indicate pointers?

                  同样:

                  const string &GetMethodName() { ... }
                  

                  & 在这里做什么?是否有一些网站解释了 & 在 C 和 C++ 中的使用方式不同?

                  What is the & doing here? Is there some website that explains how & is used differently in C vs C++?

                  推荐答案

                  &"表示引用而不是指向对象的指针(在您的情况下为常量引用).

                  The "&" denotes a reference instead of a pointer to an object (In your case a constant reference).

                  拥有

                  foo(string const& myname) 
                  

                  结束

                  foo(string const* myname)
                  

                  是在前一种情况下,您可以保证 myname 是非空的,因为 C++ 不允许 NULL 引用.由于您是通过引用传递,因此不会复制对象,就像传递指针一样.

                  is that in the former case you are guaranteed that myname is non-null, since C++ does not allow NULL references. Since you are passing by reference, the object is not copied, just like if you were passing a pointer.

                  你的第二个例子:

                  const string &GetMethodName() { ... }
                  

                  允许您返回对例如成员变量的常量引用.如果您不希望返回副本,并且再次保证返回的值是非空的,这将很有用.例如,以下内容允许您直接进行只读访问:

                  Would allow you to return a constant reference to, for example, a member variable. This is useful if you do not wish a copy to be returned, and again be guaranteed that the value returned is non-null. As an example, the following allows you direct, read-only access:

                  class A
                  {
                    public:
                    int bar() const {return someValue;}
                    //Big, expensive to copy class
                  }
                  
                  class B
                  {
                  public:
                   A const& getA() { return mA;}
                  private:
                   A mA;
                  }
                  void someFunction()
                  {
                   B b = B();
                   //Access A, ability to call const functions on A
                   //No need to check for null, since reference is guaranteed to be valid.
                   int value = b.getA().bar(); 
                  }
                  

                  您当然必须小心不要返回无效的引用.编译器会很乐意编译以下内容(取决于您的警告级别以及您对待警告的方式)

                  You have to of course be careful to not return invalid references. Compilers will happily compile the following (depending on your warning level and how you treat warnings)

                  int const& foo() 
                  {
                   int a;
                  
                   //This is very bad, returning reference to something on the stack. This will
                   //crash at runtime.
                   return a; 
                  }
                  

                  基本上,您有责任确保返回的引用实际上有效.

                  Basically, it is your responsibility to ensure that whatever you are returning a reference to is actually valid.

                  这篇关于'&' 是什么意思在 C++ 声明中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  • <small id='3FupP'></small><noframes id='3FupP'>

                      <bdo id='3FupP'></bdo><ul id='3FupP'></ul>

                      <i id='3FupP'><tr id='3FupP'><dt id='3FupP'><q id='3FupP'><span id='3FupP'><b id='3FupP'><form id='3FupP'><ins id='3FupP'></ins><ul id='3FupP'></ul><sub id='3FupP'></sub></form><legend id='3FupP'></legend><bdo id='3FupP'><pre id='3FupP'><center id='3FupP'></center></pre></bdo></b><th id='3FupP'></th></span></q></dt></tr></i><div id='3FupP'><tfoot id='3FupP'></tfoot><dl id='3FupP'><fieldset id='3FupP'></fieldset></dl></div>
                            <tbody id='3FupP'></tbody>
                          <tfoot id='3FupP'></tfoot>
                        1. <legend id='3FupP'><style id='3FupP'><dir id='3FupP'><q id='3FupP'></q></dir></style></legend>