对象和实例之间的区别:C ++ [英] Difference between Object and instance : C++

查看:104
本文介绍了对象和实例之间的区别:C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Person name;

name是类person的对象.

实例化时成为实例:

name=new Person();

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

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

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

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

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

推荐答案

在C ++中,对象"和实例"几乎可以互换使用.

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

classinstance的通用编程设计模式. class保存该class中所有instance的信息.

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

在C ++中,当您声明classstruct时,编译器将编写代码来描述如何创建该classinstance,数据布局是什么,并提供一些可以使用的方法.与该instance互动(直至并包括破坏).

In C++ when you declare a class or struct, the compiler makes code that describes how you create an instance of 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).

virtual方法和继承似乎将某些方法和布局移至实例:但是数量非常有限.而是,每个实例都包含指向virtual类数据的指针.在某些语言中,您可以执行一些操作,例如在运行时替换实例的单个方法:但在C ++中则不能.

virtual methods 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 virtual class data. In some languages, you can do things like replace individual methods of an instance at runtime: but not in C++.

创建该classstruct的实例时,可以通过堆栈上的一个自动命名变量(例如Foo f;),一个匿名自动命名变量(例如some_function( Foo(17,22) )),一个实例在免费商店中(例如new Foo(17, 22)),或通过Placement- new(这是std::vectorstd::make_shared创建实例的方式).

When you create an instance of that class or 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::vector and std::make_shared creates instances).

令人困惑的是,C ++中有一个单独的并行class-instance模式-class template-class. class templateclass,实例化是实例. template参数和特化指示在编译时如何构造" class es. class template上的模式匹配提供了与实例无关的有限数量的属性(模式中的类属性"). (可以说,函数template-function是该模式的另一个实例.)

Confusingly, there is a separate parallel class-instance pattern in C++ -- class template-class. The class template is the class, the instantiation is the instance. The template arguments 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).

如果您查看 C ++ 1y精简概念提案您将看到对象和实例在C ++中可能意味着不同的地方.

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

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

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

x is both an object and an instance of the type int.

foo是类型int&的实例,但是调用foo对象可能是错误的!它是一个引用-别名或某个对象的其他名称(在本例中为x).

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

bar是指向int的指针,该指针是类型为int*的实例,将其称为对象可能是正确的.

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

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

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.

现在,某些类型具有引用语义",因为它们在许多方面的行为都类似于引用,但实际上是class es.这种类型的实例是否更好地称为引用或对象?在可怕的情况下,某些情况下混合使用了引用语义和对象语义:这通常是一个不好的信号.

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.

通过3.9 [Types]中的最新标准在C ++中具有各种类型.它们描述了对象类型是什么:

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

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

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

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

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

因此,将函数类型或引用类型的对象的实例"称为对象"似乎是不正确的.请注意,访问函数或引用实例的表示形式"基本上是不可能的:将别名引用到它们所引用的对象中,并且使用函数名称会在帽子放下时衰减为指向函数的指针(并且函数指针基本上是不透明的句柄,可让您调用它们.)

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.

另一方面,我们讨论了class template s和函数template s的实例化. 14.7是模板实例化和专门化",实例化点(template的)都是标准的正式术语.

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

这篇关于对象和实例之间的区别:C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆