views:

816

answers:

7

For me instance variables are simple data types like int or double. Everything that is created automatically when the object is created.

If an object creates additional objects - like everything that is it done with the NEW keyword - these are not instance variables.

Am I right or wrong?

+14  A: 

Wrong. Anything that is bound within the instance is instance variable. As opposite of static (class) variables, which are bound to the class. I doesn't mater if they are simple types or pointers to objects.

vartec
+1, tentatively. The *pointers to objects* are instance variables, the *objects* they point to are not.
paxdiablo
i think vartec is right. that's the language-agnostic answer. even if the pointed to object is not a member in the c++ sense, it is still bound to it.
Johannes Schaub - litb
@Pax: true, but if you have object on heap, then the pointer is the only way to access.
vartec
A: 

That depends on when and where the object creates them. If they are declared at class level, but only created after instantiation, they are still instance variables. If they are both declared and created inside a function, they are local variables, and not instance variables.

ProfK
+1  A: 

Instance variables (aka. fields) are variables that belong to an instance, as opposed to static variables that belong to a class and local variables that belong to the local stack frame.

Your definition defines an object which is an instance of a type.

Mehrdad Afshari
+4  A: 

From Wikipedia (you asked for an exact definition):

In object-oriented programming with classes, an instance variable is a variable defined in a class, for which each object in the class has a separate copy.

An instance variable is the opposite of class variable, and it is a special type of instance member.

George Stocker
+3  A: 

Instance variable are the ones which can be associated with an instance of a class. For example if you have

class A
{
private:
int m_a;
double m_b;
int* m_c;
};

and if you create an object (i.e. an instance) of A, one instance of m_a, m_b, m_c is created and associated with it. So these become instance variables. At the same time if you have a static variable inside the class, the static variable instance is not associated with each object of the class hence it is not an instance variable. NEW or creating a stack object are just ways of creating the objects and as such have nothing to do with instance variables.

Naveen
+1  A: 
class A {
    int a;
    Foo *f;
    static int b;
};

a is an instance variable. b is not. Pointer f is an instance variable itself, the object pointed by f (created with new) is not an instance variable, because it is not even a variable, even though it is still a part of the instance state.

Alex B
+1 for differentiating the pointer and object, but the objects are still variables. I can envisage f being a pointer to the first node on a linked list, set with A::add(Foo*) [i.e., created outside of A's scope] - *all* the nodes in that list would be variables but neither instance nor class ones.
paxdiablo
But these are just *other* variables pointing to/referencing the same object. Object is just bytes in memory. There may be variables pointing to the same object outside of A's scope, but *these* are the variables, not the object.
Alex B
standard says "a variable is introduced by the declaration of an object. The variable's name denotes the object". if you new some object, then that object wasn't declared. i think checkers is right. an object is just a range of memory (but always with a type associated in C++, contrary to C)
Johannes Schaub - litb
+1  A: 

I am new to OOP concepts, but I will try my best.

Yes, Instance Variables are variables with normal datatypes BUT they BELONG to a specific instance of OBJECT. An Instance Variable is a variable that describes "Characteristic" or "Property" of an object. e.g. carColor, carName could be a Instance Variable of class "Car" since it describes a Characteristic of object car.

When a new object is instantiated with the keyword "new" all the instance variables are automatically attached to the object and can be tracked seprately. e.g.

var carA = new car carA.carName = "Honda" carA.carColor= "Blue"

var carB = new car carA.carName = "Austin" carA.carColor= "Red"