C++ 实例变量的确切定义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/743697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:04:35  来源:igfitidea点击:

What is the exact definition of instance variable?

c++classinstance-variables

提问by TalkingCode

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

我认为实例变量是简单的数据类型,如 int 或 double。创建对象时自动创建的所有内容。

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

如果一个对象创建了额外的对象——就像它用 NEW 关键字所做的一切一样——这些不是实例变量。

Am I right or wrong? What is the exact definition?

我是对还是错?确切的定义是什么?

回答by vartec

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.

错误的。任何绑定在实例中的东西(即实例化的对象)都是实例变量。与绑定到类的静态(类)变量相反。它们是简单类型还是指向对象的指针并不重要。

回答by Naveen

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.

如果您创建 A 的一个对象(即实例),则会创建 m_a、m_b、m_c 的一个实例并与之关联。所以这些成为实例变量。同时,如果类中有静态变量,则静态变量实例不与类的每个对象相关联,因此它不是实例变量。NEW 或创建堆栈对象只是创建对象的方法,因此与实例变量无关。

回答by George Stocker

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.

在类的面向对象编程中,实例变量是在类中定义的变量,类中的每个对象都有一个单独的副本。

实例变量与类变量相反,是一种特殊类型的实例成员。

回答by TalkingCode

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

我是 OOP 概念的新手,但我会尽力而为。

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.

是的,实例变量是具有普通数据类型的变量,但它们属于 OBJECT 的特定实例。实例变量是描述对象“特性”或“属性”的变量。例如,carColor、carName 可以是类“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.

当使用关键字“new”实例化一个新对象时,所有实例变量都会自动附加到该对象并且可以单独跟踪。例如

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

var carA = 新车 carA.carName = "Honda" carA.carColor = "Blue"

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

var carB = 新车 carA.carName = "Austin" carA.carColor="Red"

回答by Alex B

class A {
    int a;
    Foo *f;
    static int b;
};

ais an instance variable. bis not. Pointer fis 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.

a是一个实例变量。b不是。指针f本身就是一个实例变量,所指向f对象(用new)不是一个实例变量,因为它甚至不是一个变量,即使它仍然是实例状态的一部分。

回答by Mehrdad Afshari

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

实例变量(又名字段)是属于实例的变量,而不是属于类的静态变量和属于本地堆栈帧的局部变量。

Your definition defines an objectwhich is an instance of a type.

您的定义定义了一个对象,它是一个类型的实例。

回答by ProfK

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.

这取决于对象在何时何地创建它们。如果它们在类级别声明,但仅在实例化后创建,它们仍然是实例变量。如果它们都在函数内声明和创建,则它们是局部变量,而不是实例变量。