对象和实例的区别:C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22206044/
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 23:59:50  来源:igfitidea点击:

Difference between Object and instance : C++

c++oopobjectinstance

提问by joey rohan

I followed a number of posts on SO, and finally I can draw a conclusion that when we have something like :

我关注了一些关于 SO 的帖子,最后我可以得出一个结论,当我们有类似的东西时:

Person name;

nameis an object of class person.

name是类的对象person

It becomes instance when instantiate it :

它在实例化时成为实例:

name=new Person();

I am a beginner in C++, and so far I have seen we can access the functions and variables like:

我是 C++ 的初学者,到目前为止我已经看到我们可以访问如下函数和变量:

Person name;
name.getValue;
name.callFunction();

We need not to use newoperator for this. So can we say the differentiating factor between an object and instance can be ignored in C++?

我们不需要为此使用new运算符。那么我们可以说在 C++ 中可以忽略对象和实例之间的区分因素吗?

回答by Yakk - Adam Nevraumont

In C++ "object" and "instance" are used nearly interchangably.

在 C++ 中,“对象”和“实例”几乎可以互换使用。

There is a general programming design pattern of classand instance. The classholds the information about all instances in that class.

有一个通用的编程设计模式classinstance。Theclass包含有关所有instances的信息class

In C++ when you declare a classor struct, the compiler makes code that describes how you create an instanceof that class, what the data layout is, and provides some methods that can be used to interact with that instance(up to and including destruction).

在 C++ 中,当您声明 aclass或 时struct,编译器会生成描述您如何创建 ainstance的代码、class数据布局是什么,并提供一些可用于与之交互的方法instance(直到并包括销毁)。

virtualmethods and inheritance seemingly moves some of the methods and layout to the instance: but the amount is quite limited. Instead, each instance holds pointers to virtualclass data. In some languages, you can do things like replace individual methods of an instance at runtime: but not in C++.

virtual方法和继承看似将一些方法和布局移动到实例中:但数量非常有限。相反,每个实例都持有指向virtual类数据的指针。在某些语言中,您可以执行诸如在运行时替换实例的单个方法之类的操作:但在 C++ 中则不能。

When you create an instance of that classor struct, it can be via an automatic named variable on the stack (like Foo f;), an anonymous automatic named variable (like some_function( Foo(17,22) )), an instance on the free store (like new Foo(17, 22)), or via placement-new(which is how std::vectorand std::make_sharedcreates instances).

当您创建classor的实例时struct,它可以通过堆栈上的自动命名变量(如Foo f;)、匿名自动命名变量(如some_function( Foo(17,22) ))、自由存储上的实例(如new Foo(17, 22))或通过放置new(即如何std::vectorstd::make_shared创建实例)。

Confusingly, there is a separate parallel class-instancepattern in C++ -- class template-class. The class templateis the class, the instantiation is the instance. The templatearguments and specializations indicate how, at compile time, you can "construct" the classes. Pattern matching on the class templates provide a limited amount of properties that are not tied to the instances ("class properties" in the pattern). (Arguably the function template-function is another instance of the pattern).

令人困惑的是,有一个单独的并行class-instance图案在C ++ - - 。class templateclassclass templateclass,该实例是实例。该template参数和专业化指示如何,在编译时,可以在“结构”的classES。class templates上的模式匹配提供了有限数量的与实例无关的属性(模式中的“类属性”)。(可以说函数模板函数是该模式的另一个实例)。

If you look at the C++1y proposal for concepts liteyou will see where object and instance might mean different things in C++.

如果您查看C++1y 概念 lite 提案,您将看到对象和实例在 C++ 中可能意味着不同的事物。

int x = 0;
int& foo = x;
int* bar = &x;

xis both an object and an instance of the type int.

x既是对象又是类型的实例int

foois an instance of the type int&, but calling fooan object is probably wrong! It is a reference -- an alias, or a different name for some object (in this case x).

foo是 type 的一个实例int&,但是调用foo一个对象可能是错误的!它是一个引用——一个别名,或某个对象的不同名称(在本例中x)。

baris a pointer to an int, which is an instance of type int*, and calling it an object is probably correct.

bar是指向 an 的指针int,它是 type 的一个实例int*,将其称为对象可能是正确的。

This is a useful distinction: a type does not have to denote an object type if it is a reference type. Object types behave differently than reference types in a number of important ways.

这是一个有用的区别:如果类型是引用类型,则它不必表示对象类型。对象类型在许多重要方面的行为与引用类型不同。

Now, some types have "reference semantics", in that they behave like references in many ways, but are actually classes. Are instances of such a type better called references or objects? In horrible cases, some instances have a mixture of both reference and object semantics: such is often a bad sign.

现在,某些类型具有“引用语义”,因为它们在许多方面的行为类似于引用,但实际上是classes。这种类型的实例是更好地称为引用还是对象?在可怕的情况下,一些实例混合了引用和对象语义:这通常是一个不好的迹象。

Via latest standardin 3.9 [Types] we have the kinds of types in C++. They describe what an object typeis:

通过3.9 [Types] 中的最新标准,我们拥有 C++ 中的类型种类。它们描述了什么是对象类型

Types describe objects (1.8), references (8.3.2), or functions (8.3.5)

类型描述对象 (1.8)、引用 (8.3.2) 或函数 (8.3.5)

and

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.

对象类型是(可能是 cv 限定的)类型,它不是函数类型,不是引用类型,也不是 void 类型。

So calling the "instances" of things that are function types or reference types "objects" seems incorrect. Note that accessing the "representation" of a function or a reference instance is basically impossible: references alias into the object they refer to, and using the name of a function decays to a pointers-to-functions at the drop of a hat (and pointers-to-a-function are basically opaque handles that let you invoke them).

因此,将函数类型或引用类型的事物的“实例”称为“对象”似乎是不正确的。请注意,访问函数或引用实例的“表示”基本上是不可能的:将别名引用到它们所引用的对象中,并且使用函数的名称会逐渐衰减为指向函数的指针(和指向函数的指针基本上是不透明的句柄,可让您调用它们)。

So arguably functions are not instances, and references are not instances.

所以可以说函数不是实例,引用也不是实例。

On the third hand, we do talk about instantiations of classtemplates and function templates. 14.7 is "template instantiation and specialization", and points of instantiation (of a template) are all formal terms from the standard.

另一方面,我们确实讨论了classtemplates 和函数templates 的实例化。14.7 是“模板实例化和专业化”,实例化点( a template)都是标准中的正式术语。

回答by Christian Hackl

First, you should know that there is no difference between "object" and "instance". They are synonyms. In C++, you also call instances of primitive types like intor double"objects". One of the design principles of C++ is that custom types (i.e. classes) can be made to behave exactly like primitive types. In fact, in C++, one often prefers to refer to "types" and not "classes".

首先,您应该知道“对象”和“实例”之间没有区别。它们是同义词。在C ++中,你还叫喜欢原始类型的实例intdouble“对象”。C++ 的设计原则之一是可以使自定义类型(即类)的行为与原始类型完全相同。事实上,在 C++ 中,人们通常更喜欢引用“类型”而不是“类”。

So, typesand objectsit shall be. Now that we've settled this, I'm afraid I must tell you that your conclusions are wrong.

所以,类型对象应该是。既然我们已经解决了这个问题,恐怕我必须告诉你,你的结论是错误的。

Personis a type. nameis a (not very well named) variable to access an objectof that type.

Person是一种类型name是一个(不是很好命名的)变量来访问该类型的对象

A whole line of C++ code would look like this:

一整行 C++ 代码如下所示:

Person name;

This means: "create an object of type Person and let me access it via the name variable".

这意味着:“创建一个 Person 类型的对象,让我通过 name 变量访问它”。

new Person()is much more complicated. You may be familiar with the newkeyword from languages like Java, but in C++, it's a very different beast. It means that a new object of type Personis created, but it also means that you are responsible for destroying itlater on. It also gives you a different kind of handle to the newly created object: a so-called pointer. A Personpointer looks like this:

new Person()要复杂得多。您可能熟悉newJava 等语言中的关键字,但在 C++ 中,它是一个非常不同的野兽。这意味着创建了一个新的类型对象Person,但也意味着您有责任稍后销毁它。它还为您提供了一种新创建对象的不同句柄:所谓的指针。一个Person指针看起来像这样:

Person*

A pointer is itself a type, and the types Person*and Personare not compatible. (I told you that this would be much more complicated :))

指针本身是一个类型,类型Person*Person不兼容。(我告诉过你这会复杂得多:))

You will notice the incompatibility when you try to compile the following line:

当您尝试编译以下行时,您会注意到不兼容:

Person name = new Person();

It won't compile; you will instead receive an error message. You'd have to do it like this instead:

它不会编译;您将收到一条错误消息。你必须这样做:

Person* name_ptr = new Person();

And then you'd have to access all the members of Personwith a different syntax:

然后您必须Person使用不同的语法访问所有成员:

name_ptr->getValue();
name_ptr->callFunction();

Finally, remember you must explicitly destroy the object in this case:

最后,请记住在这种情况下您必须显式销毁对象:

delete name_ptr;

If you forget this, bad things can happen. More precisely, your program will likely use more and more memory the longer it runs.

如果您忘记了这一点,可能会发生不好的事情。更准确地说,您的程序运行时间越长,可能会使用越来越多的内存。

I think that pointers are too advanced yet for your level of C++ understanding. Stay away from them until you actually need them.

我认为指针对于您的 C++ 理解水平来说太先进了。远离它们,直到你真正需要它们。

回答by Jose

Object and instance are two words for the same thing.

对象和实例是同一事物的两个词。

回答by Joseph Mansfield

"Object" and "instance" are almost interchangeable. In C++, an object is formally any region of storage. "Instance" is not a formally defined term, but we typically refer to "instances of type X", most commonly used with class types.

“对象”和“实例”几乎可以互换。在 C++ 中,对象形式上是任何存储区域。“实例”不是一个正式定义的术语,但我们通常指的是“类型实例X”,最常用于类类型。

Foo f;

This declaration creates an object named f. The object's type is Foo. You could say the object fis an instance of Foo.

该声明创建了一个名为 的对象f。对象的类型是Foo。您可以说该对象fFoo.

Your attempt to distinguish the terms was incorrect. The two things you've actually pointed out are two different ways of creating objects.

您试图区分这些术语是不正确的。您实际上指出的两件事是创建对象的两种不同方式。

Person name;

In this case, we're creating an object nameof type Person.

在这种情况下,我们正在创建一个name类型为 的对象Person

Person* name = new Person();

In this case, we're creating an object nameof type Person*(pointer to Person). We are also creating another object of type Personusing the expression new Person(). This expression returns a pointer, which we are initialising the nameobject with.

在这种情况下,我们正在创建一个name类型Person*(指向Person)的对象。我们还Person使用表达式创建了另一个类型的对象new Person()。这个表达式返回一个指针,我们name用它来初始化对象。

回答by sushanth

It is very simple but very important

这很简单但很重要

Take a general example: what is the general meaning of object? its nothing but which occupies some space right....keep that in mind we now talk about Object in java or C++

举个一般的例子:object的一般含义是什么?它只是占据了一些空间......请记住,我们现在谈论的是 Java 或 C++ 中的对象

example: here I am creating Object     Student std=new Student();

示例:这里我正在创建对象     学生 std=new Student();

where Studentis a Classand stdis a Objectbecause we Created a memory for std with the help of newkeyWord it means it internally occupies some space in memory right thats y we call stdas Object

其中Student是一个std是一个对象,因为我们在关键字的帮助下为std创建了一个内存,这意味着它在内部占用了一些内存空间,这就是我们称std对象的地方

if you won`t create memory for an object so we call that objectas instance.

如果您不会为对象创建内存,则我们称该对象instance

example: Student std;

示例:学生标准;

here Studentis a class and stdis a instance(means a just a copy that class),with this we won`t do anything untill unless we create a memory for that.

这里Student是一个类,std是一个实例(意味着只是该类的副本),除非我们为此创建内存,否则我们不会做任何事情。

Thats all about object and Instance :)

这就是对象和实例的全部内容:)