在 c++ 中定义变量

发布于 2024-12-09 14:00:17 字数 374 浏览 2 评论 0原文

我总是对“定义变量”的概念感到困惑。定义是什么意思?

例如:

void main {
map<int,int> *infoMap;

if() {
//some check here,if it passes this check, then new infoMap
}

infoMap = new infoMap; 

}

那么

map<int,int> *infoMap;

or

map<int,int> *infoMap = new inforMap;

定义一个变量吗?

I am always confused with the concept "define variable." What does define mean?

For example:

void main {
map<int,int> *infoMap;

if() {
//some check here,if it passes this check, then new infoMap
}

infoMap = new infoMap; 

}

So does

map<int,int> *infoMap;

or

map<int,int> *infoMap = new inforMap;

define a variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月亮是我掰弯的 2024-12-16 14:00:17

最上面的是声明,或者如果您愿意的话,定义。在这里,编译器为变量分配空间。

最下面的是一个作业。在这里,编译器填充在定义时分配的空间。如果您想将变量的值更改为其他值,则可以进行多个赋值。

The top one is the declaration, or if you like, definition. Here, the compiler allocates space for the variable.

The bottom one is an assignment. Here, the compiler fills the space it allocated at definition time. You can have more than one assignment, if you want to change the value of the variable to something else.

疏忽 2024-12-16 14:00:17

这是一个随机的、不完整的说明:

class Foo;   // (incomplete forward) class declaration

class Foo {  //  class definition
  int a;           //  member declaration + definition
  int b(int, int); //  member function declaration
  static int c;    //  static member declaration
};

int Foo::b(int a, int b) { return a+b; }  // member function definition
int Foo::c;                               // static member defintion

int bar(int);   // free function declaration

int main() {    // free function declaration + definition
  int q;        // declaration + definition
  q = bar(0);
  return q;
}

int bar(int a) { return 2 * a; }  // free function definition

也许“静态成员定义”是唯一的,因为它为已在其他地方声明的对象提供了实际的对象实例(即分配加构造)。这只能与纯外部声明相比较:

extern int N;  // declaration only, no definition

不要与具有外部可见性的声明+定义相混淆:

extern const int M = 11;  // declaration and definition

Here's a random, incomplete illustration:

class Foo;   // (incomplete forward) class declaration

class Foo {  //  class definition
  int a;           //  member declaration + definition
  int b(int, int); //  member function declaration
  static int c;    //  static member declaration
};

int Foo::b(int a, int b) { return a+b; }  // member function definition
int Foo::c;                               // static member defintion

int bar(int);   // free function declaration

int main() {    // free function declaration + definition
  int q;        // declaration + definition
  q = bar(0);
  return q;
}

int bar(int a) { return 2 * a; }  // free function definition

Perhaps the "static member definition" is unique in the sense that it provided an actual object instance (i.e. allocation plus construction) for an object that has been declared elsewhere. This is only comparable to a pure-extern declaration:

extern int N;  // declaration only, no definition

Not to be confused with a declaration+definition with external visibility:

extern const int M = 11;  // declaration and definition
梦冥 2024-12-16 14:00:17
map<int,int> *infoMap;

infoMap 是声明。通常,当初始化和声明一起存在时,称为定义变量。

map<int,int> *infoMap;

infoMap is the declaration. Usually when there is initialization along with declaration then it's called defining the variable.

桃扇骨 2024-12-16 14:00:17

在 C++ 中定义某些东西就是将一个标识符(在本例中是一个指向 map 的指针)绑定到某个存储,这与仅将标识符绑定到类型和类型的声明相反。不分配存储。如果编译器不需要有关类型定义的任何信息(即它只需要类型),那么您只需声明即可。否则你需要一个定义。

To define something in C++ is to bind an identifier (in this case a pointer to a map<int, int>) to some storage, as opposed to a declaration that only binds an identifier to a type and does not allocate storage. If the compiler does not need any information about the definition of a type (i.e. it only needs the type), then you can get away with just a declaration. Otherwise you need a definition.

我只土不豪 2024-12-16 14:00:17

变量的定义和声明往往可以互换使用。然而,有一个细微的差别。

在大多数情况下,您实际上是在定义变量。

map<int,int> *infoMap;

术语“定义”声明一个符号并赋予它实质内容、变量的存储空间、结构/类主体、函数实现。

在某些情况下,您可以使用 extern 关键字“声明”变量。这基本上通知编译器符号名称及其类型的存在,但不为其分配空间。该空间分配在实际定义变量的其他地方。

// foo.c
extern int bar;

// bar.c
int bar;

With variables define and declare tend to be used interchangeably. However there is a subtle difference.

In most cases you are actually defining the variable.

map<int,int> *infoMap;

The term "define" declares a symbol and gives it substance, storage space for variable, structure/class body, function implementation.

In some instances you can "declare" the variable using the extern keyword. This basically informs the compiler of the existence of the symbol name and its type but does not allocate space for it. The space is allocated elsewhere where the variable is actually defined.

// foo.c
extern int bar;

// bar.c
int bar;
毁虫ゝ 2024-12-16 14:00:17

当您在 C++ 中声明变量时,您会为其保留内存空间,但内存不会被写入。这个声明发生在

map<int,int> *infoMap;

它可以包含任何东西。当您定义变量时,您实际上将其设置为某个值,在本例中是一个新的 infoMap。

When you declare a variable in c++ you reserve space in memory for it, but the memory is not written to. This declaration happens in

map<int,int> *infoMap;

It could contain anything. When you define the variable you actually set it to some value, in this case a new infoMap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文