函数签名Function Signature

功能签名

    函数签名由函数原型组成。它告诉你的是关于函数的一般信息,它的名称,参数,它的范围以及其他杂项信息。 C ++“mangle”的函数名称使它们很漂亮,尽管事实上它们可能非常难看。另一方面,不管是否存在错误,函数签名中存在大量信息(存储在大多数编译器内部),这些信息不易获得,但可以访问。考虑以下功能:

  static const int&Foo :: Run(World&W,PrioQ&pq)const;
---这是相同的(虽然这个函数的签名在上面......因为这是你必须用它来定义它。):
  class Foo {
    
     
      static const int&Run(World&W,PrioQ&pq)const;
    
  };

功能签名是上面的那个。如何知道这一点,因为为了定义函数,必须使用签名...虽然上述签名是什么意思?

  static const int&Foo :: Run(World&W,PrioQ&pq)const;

static  - 这个函数只能在这个文件中看到?不,这意味着可以在没有实例化对象的情况下调用此函数,因为通常必须使用类的实例化来调用成员函数(方法),但是使用此关键字,您不需要它。

const  - 此函数返回的int无法更改,因为此函数返回对int的引用,您通常可以更改它,但此特定函数不允许更改。

int& - 这是函数的返回类型。这个特殊的函数返回一个int,但不仅如此,它是对int的引用,因此它实际上不是一个int,而是表现为一个。

Foo ::  - 这个函数实际上是在范围Foo中声明的函数,它是一个类。

运行 - 函数的名称。

World& - 第一个参数的类型。这对于大型对象来说是典型的,因为如果它们是按值传递的,那么必须复制整个对象(可能很大)(如果对象很大,复制它可能需要很长时间)并且引用允许一个修改传入的对象。

W  - 这是函数用于引用它的第一个参数的名称。

PrioQ& - 这是第二个参数的类型。这对于大型对象来说是典型的,因为如果它们是按值传递的,那么必须复制整个对象(可能很大)(如果对象很大,复制它可能需要很长时间)并且引用允许一个修改传入的对象。

pq  - 这是第二个参数的名称,该函数将用于引用它。

const  - 虽然这看起来像这个函数前面的const,但它不一样。函数末尾的const与开头的const不同。此const阻止此函数更改它所属的类的成员变量(在本例中为Foo)。

    A function signature consists of the function prototype.  What it tells you is the general information about a function, its name, parameters, what scope it is in, and other miscellaneous information.  C++ "mangle"s function names so that they are pretty, though in all truth they can be very ugly.  On the other hand, mangling or not there is alot of information in a function signature (which is stored internally to most compilers) which is not readily available, but can be accessed.  Consider the following function:

  static const int & Foo::Run(World& W, PrioQ& pq) const; 
--- which is the same as (though this function's signature is above...as this is what you must use to define it.): 
  class Foo { 
    public: 
    -- majorly snipped of code-- 
      static const int & Run(World& W, PrioQ & pq) const; 
    -- more snipping... -- 
  };

The function signature is the one above.  How does one know this, because in order to define the function, one must use the signature...  Though what does the above signature mean?

  static const int & Foo::Run(World& W, PrioQ& pq) const;

static -- this function can only be seen in this file?  No, this means that this function can be called without an instantiated object, as normally member functions (methods) must be called using an instantiation of the class, though with this keyword, you don't need it.

const -- the int that this function returns cannot be changed, because this function returns a reference to an int, you could normally change it, but this particular function does not allow that.

int & -- This is the return type of the function.  This particular function returns an int, but not only that, but it is a reference to an int, so it is not really an int, but behaves as one.

Foo:: -- This function is really a function declared in the scope Foo, which is a class.

Run -- The name of the function.

World& -- The type of the first argument.  This is typical for large objects, as, if they are passed by value, then the entire object (which can be huge) must be copied (if the object is huge, copying it can take a long time) and the reference allows one to modify the object being passed in.

W -- This is the name of the first parameter that the function will use to refer to it.

PrioQ & -- This is the type of the second argument.  This is typical for large objects, as, if they are passed by value, then the entire object (which can be huge) must be copied (if the object is huge, copying it can take a long time) and the reference allows one to modify the object being passed in.

pq -- This is the name of the second parameter, that the function will use to refer to it.

const -- Though this looks like the const on the front of this function, it is not the same.  The const on the end of the function is not the same as the const at the beginning.  This const prevents this function from changing the member variables of the class to which it belongs (in this case Foo).

 

你可能感兴趣的:(C++)