Difference between static and non static data members in c++
Answers
Answered by
5
static namespace scope variables have internal linkage, while non-static namespace scope variables have external linkage by default! Details: a const namespace scope variable has internal linkage by default. That linkage can by changed by keyword extern.
static variables in a class is associated with the class, that means, all instances of the class have the same instances of the static variables; they’re like a global variables that every instance of the same class has access to.
non-static variables in a class are instance members, that is, every instance of the class will have its own instances of the non-static variables.
a static data member of a class has external linkage if the name of the class has external linkage. [$3.5/5]
a static variable inside a function retains its value even after returning from the function. That is, its lifetime is equal to the lifetime of the program itself.
static variables in a class is associated with the class, that means, all instances of the class have the same instances of the static variables; they’re like a global variables that every instance of the same class has access to.
non-static variables in a class are instance members, that is, every instance of the class will have its own instances of the non-static variables.
a static data member of a class has external linkage if the name of the class has external linkage. [$3.5/5]
a static variable inside a function retains its value even after returning from the function. That is, its lifetime is equal to the lifetime of the program itself.
Answered by
20
A Static variable will have only one single memory location in a class while a Non-static variable will have more than one or different memory location for each instance of class.
A Static member of a class has its value shared between all instances of a class.
You cannot have static and non static member functions with the same names and the same number and type of arguments.
A static member function does not have a 'this' pointer.
Similar questions
English,
7 months ago
Math,
7 months ago
India Languages,
7 months ago
Chemistry,
1 year ago
Biology,
1 year ago