I REALLY should know this (but I don't) because I've been programming for quite some time now, but I have been on and off due to school.
I think that declaring a variable is doing something like: int x = 7; then cout << "Blah, blah" << x << endl;
Correct me if I'm wrong on this.
(Although I feel like I just answered my own question)
Declaring an identifier means bringing an identifier into scope. In other words, telling the compiler that a variable with the identifier "Variable" exists, for example. Note that a definition, and a declaration of an identifier is the same thing, but the two are used interchangeably.
Initialising a variable means to give a variable a value during its declaration. The difference between initialisation and assignment is quite distinct, since initialisation only applies to a declaration context. You should be aware that initialisation is good practice.
extern int x; is declaration not a definition. The decllaration and the definition are exact notions in the C++ standard. The same is valid for a static data member declared in a class. When you are declaring a static data member you can declare it with incomplete type because it is only a declaration not definition. See my example with the static data member declared as array.
Thanks for the info, guys :D The little argument/discussion you guys were having amused me a little :) And framework, how could I practice programming with initialization? Would you mind giving some example code to work with?
int x=0; //declared and initialized
cout<<"Enter a number: ";
cin>>x;
int *temp=new[x];
cout<<x;
x is initialized, in case you make a mistake and let the user cin>> in a diferent variable. If it was not initialized, you would have gotten some error
"And framework, how could I practice programming with initialization?"
Remembering to initialise your variables is always good practice, and the more you do it, the more likely it will be your second nature. Initialisation can happen a few ways, but the way you initialise a variable depends on where, and how it was declared. Below is some cases of initialisation:
// ------------------
// Type A
// ------------------
struct Sample
{
Sample(intconst InitValue = 0)
: Member(InitValue) // Initialiser-list
{ }
int Member;
};
// If you gave "Sample::Member" a value within the body of the "Sample" constructor,
// you would be assigning a value to "Sample::Member", and not initialising it.
// ------------------
// Type B
// ------------------
struct Sample
{
staticint StaticMember;
};
int Sample::StaticMember(0); // Initialisation.
// ------------------
// Type C
// ------------------
int Variable(0); // "Variable = 0" is the same.
// ------------------
// Type D
// ------------------
int Array[3] = {1, 2, 3};