I think 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? What is the exact definition?
Wrong. Anything that is bound within the instance (i.e. an instantiated object) is instance variable. As opposite of static (class) variables, which are bound to the class. It doesn't matter if they are simple types or pointers to objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With