开发者

Declaration vs. Prototype vs. Symbol vs. Definition vs. Implementation

开发者 https://www.devze.com 2023-03-01 18:22 出处:网络
I see the terms \"declaration,\" \"prototype\" and \"symbol\" thrown around interchangeably a lot when it comes to code like the following:

I see the terms "declaration," "prototype" and "symbol" thrown around interchangeably a lot when it comes to code like the following:

void MyUndefinedFunction();

The same goes for "definition" and "implementation" for things like this:

void MyClass::MyMethod()
{
    // Actual code here.
}

Are there any distinctions between the terms, as with "argument" and "parameter?" Or are they truly synonymous?

Note: I'm not sure if this belongs here or on Programmers, so I posted it on both sites. If an开发者_开发知识库yone has any objections, let me know and I'll delete one.


Unless you run into a purist, they are generally interchangable, except for symbol and prototype (difficult to give absolutes on language-agnostic)

  • symbol generally refers to a hook point for linking 2 bits of code together, such as a library entry point, or a target for resolving static linking
  • prototype generally refers to a definition of what a function/method looks like (arguments, return type, name, various types of visibility), but doesn't include an implementation.

You missed function vs. method, but my definition is:

  • function a callable bit of code that isn't bound to an object
  • method a callable bit of code in an object's namespace. Generally implemented by the compiler as a function that takes the object instance as it's first argument.

Possibly parameter hints at limiting scope, and therefore read-only.

Note If you ask a purist, you're more likely to have an argument than a parameter.


The difference between declaration and prototype is mainly in C, where the following is a non-prototype declaration:

int foo();

Note that this is different from:

int foo(void);

The latter is a prototype for a function taking no arguments, while the former is a declaration for a function whose argument types are not specified in the declaration. This can actually be useful to avoid function-pointer-type casts with certain uses of function pointers, but it's very easy to mess up, and messing it up invokes undefined behavior. Many C programmers consider non-prototype declarations harmful, and gcc has a warning option to flag them.

0

精彩评论

暂无评论...
验证码 换一张
取 消