Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between declaration and definition of a variable in C++? [duplicate]

Tags:

My question stems from studying Effective C++ by Scott Meyers. In Item II of that book, following is written :

To limit the scope of a constant to a class, you must make it a member and, to ensure there's at most one copy of the constant, you must make it a static member.

That is correctly written. Then immediately the following example is given :

class GamePlayer {
private:
    static const int NumTurns = 5;
    int scores[NumTurns];
    ....
  };

Then the following is written pertaining to the above example :

What you see above is a declaration and not a definition of NumTurns.

My First question is : What is the meaning of this statement ?

Immediately after that the following is mentioned :

Usually C++ requires that you provide a definition for anything you use, but class specific constants that are static and of integral type (e.g - integers, chars, bools) are an exception. As long as you don't take their address, you can declare them and use them without providing a definition. If you do take the address of a class constant, or if your compiler incorrectly insists on a definition even if you don't take the address, you provide a separate definition like this : const int GamePlayer::Numturns; //definition of NumTurns

Why now it is a definition and not a declaration ?

I understand the difference in the context of a function but do not understand it in the context of a regular variable. Also, can someone expand on what the author means by

... if you do take the address of a class constant, or if your .. part of the above quoted paragraph ?

P.S : I am a relatively newbie in C++.

like image 459
Ujjwal Aryan Avatar asked Apr 29 '15 20:04

Ujjwal Aryan


People also ask

What is the difference between declaration and definition of a variable in C?

Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.

What is the difference between a definition and a declaration?

Difference Between Definition and Declaration It aims at determining the overall values stored in a class, a function, or a variable. It aims at specifying the name of any given class, function, variable, etc. Definition allocates memory to an entity. A declaration does not allocate memory to the entities.

What is the difference between declaration and definition of a variable in C Mcq?

Answer: Variable declaration tells the compiler about data type and size of the variable. Whereas, variable definition allocates memory to the variable.

What is the difference between a declaration and a definition initialization of a variable?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable.


1 Answers

Just as with functions, variables can have 'purely declarative' declarations, and actual definitions. You're confused because you probably didn't encounter many pure variable declarations before.

int i; // Definition
extern int i, j; // (Re)declares i, and declares j
extern int j = 0; // Defines j (confusing, eh?)

As you're used to with functions, definitions are declarations, but not all declarations are definitions. §3.1/2 reads

A declaration is a definition unless […] it declares a static data member in a class definition (9.2, 9.4),

Thus in-class static data member declarations are never defining the variables they declare. However, sometimes, a variables definition doesn't have to be present. That is the case when you can use its value directly without necessitating the variables run-time existence.

In technical terms, as long as a static data member (or any entity, for that matter) is not 'odr-used', it doesn't have to be defined. Odr-use for all entities is defined in §3.2/3:

A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant expression (5.20) that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e, or e is a discarded-value expression (Clause 5).

This looks complicated, and it was simpler in earlier versions of the standard. However, it roughly says that the variable is not odr-used by some expression when that expression "immediately" accesses the variables value, and this access yields a constant expression. Meyers example of "taking its address" is only one of many for odr-use.

For some class A and its static data member i,

class A {
    static const int i = 57; // Declaration, not definition
};

const int A::i; // Definition in namespace scope. Not required per se.
like image 187
Columbo Avatar answered Oct 04 '22 08:10

Columbo