开发者

When can "creating" and "declaring" be used synonymously?

开发者 https://www.devze.com 2022-12-23 22:53 出处:网络
Once in a while you stumble over a technical article where \"creating\" and \"declaring\" ar开发者_JAVA技巧e used synonymously.

Once in a while you stumble over a technical article where "creating" and "declaring" ar开发者_JAVA技巧e used synonymously.

E.g.

declares an array of ints

creates an array of ints

But aren't declaring and creating two different things? Or does it depend on the context?


The two are clearly separate, at least under some circumstances. Just for example, in C or C++, there are declarations that just declare things, and there are definitions that both declare and create them, and (only in C++) there are new expressions that create objects without declaring them (and, arguably, a malloc sort of does the same in C).

Likewise, in a language that supports lambda expressions, creation and declaration are separate -- a lambda expression creates something (e.g., a function) but doesn't (by itself) declare it (bind a name to it).


In C and C++, the creation of an object also declares it:

int main() {
    int a[10];   // create a
    a[3] = 42;   // use a - no need for a separate declaration
}
0

精彩评论

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