C,C ++,C#,Java和Python中的声明,定义,初始化 [英] Declarations, definitions, initializations in C, C++, C#, Java and Python

查看:73
本文介绍了C,C ++,C#,Java和Python中的声明,定义,初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以上每种语言中的术语是什么意思?为什么这方面的语言会有所不同(无论在哪里,如果有的话)?

What do the terms mean in each of the above languages? Why do the languages differ (wherever they do, if at all they do) in this respect?

推荐答案

C/C ++:

声明是一条声明,内容为这里是事物的名称和事物的类型,但我不会再告诉您任何东西".

C/C++:

A declaration is a statement that says "here is the name of something and the type of thing that it is, but I'm not telling you anything more about it".

定义是一条语句,上面写着这里是某物的名称以及它的确切含义".对于函数,这将是函数主体;对于全局变量,这将是变量所在的翻译单元.

A definition is a statement that says "here is the name of something and what exactly it is". For functions, this would be the function body; for global variables, this would be the translation unit in which the variable resides.

初始化是一个定义,其中变量也被赋予了初始值.某些语言会自动将所有变量初始化为某个默认值,例如0,false或null.有些(例如C/C ++)并非在所有情况下都行:所有全局变量都是默认初始化的,但是堆栈上的局部变量和堆上的动态分配的变量不是默认初始化的-它们具有未定义的内容,因此您必须显式初始化他们. C ++还具有默认构造函数,这是蠕虫的全部功能.

An initialization is a definition where the variable is also given an initial value. Some languages automatically initialize all variables to some default value such as 0, false, or null. Some (like C/C++) don't in all cases: all global variables are default-initialized, but local variables on the stack and dynamically allocated variables on the heap are NOT default initialized - they have undefined contents, so you must explicitly initialize them. C++ also has default constructors, which is a whole nother can of worms.

示例:


// In global scope:
extern int a_global_variable;  // declaration of a global variable
int a_global_variable;         // definition of a global variable
int a_global_variable = 3;     // definition & initialization of a global variable

int some_function(int param);  // declaration of a function
int some_function(int param)    // definition of a function
{
    return param + 1;
}

struct some_struct;  // declaration of a struct; you can use pointers/references to it, but not concrete instances
struct some_struct   // definition of a struct
{
    int x;
    int y;
};

class some_class;  // declaration of a class (C++ only); works just like struct
class some_class   // definition of a class (C++ only)
{
    int x;
    int y;
};

enum some_enum;  // declaration of an enum; works just like struct & class
enum some_enum   // definition of an enum
{
   VALUE1,
   VALUE2
};

我对您询问的其他语言不太熟悉,但是我相信它们在声明和定义之间没有太大的区别. C#和Java对所有对象都有默认的初始化-如果未显式初始化,则一切都初始化为0,false或null. Python更加宽松,因为在使用变量之前不需要声明变量.由于绑定是在运行时解析的,因此也不需要真正的函数声明.

I'm not as familiar with the other languages you asked about, but I believe they don't make much of a distinction between declarations and definitions. C# and Java have default initialization for all objects - everything is initialized to 0, false, or null if you don't explicitly initialize it. Python is even more lax, since variables don't need to be declared before they're used. Since bindings are resolved at runtime, there's no real need for declarations of functions, either.

这篇关于C,C ++,C#,Java和Python中的声明,定义,初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆