Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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
map<int,int> *infoMap;

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

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.

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;

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top