什么可以实例化? [英] What can be instantiated?

查看:95
本文介绍了什么可以实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以实例化C ++中的什么类型?



我知道以下每个都直接创建 Foo

  Foo bar; 
Foo * bizz = new Foo();

但是,对于内置类型呢?下面是否创建 int 的两个实例,或者是使用错误的单词并且正在分配内存?

  int bar2; 
int * bizz2 = new int;

指针呢?上面的例子创建了一个 int * 实例,或者只为 int * 分配内存?



使用 42 3.14 的文字创建实例吗? / p>

我已经看到了如果你不能继承一个类型的参数,它不是一个类,如果它不是一个类,它不能被实例化。这是真的吗?

解决方案

只要我们谈论C ++,唯一的权威来源是ISO标准。除了类和函数模板之外,它不会使用单词instantiation。



但是,它使用单词instance。例如:


具有自动存储持续时间(3.7.2)的每个对象的实例与其块中的每个条目相关联。注意,在C ++语言中, int lvalue也是一个对象:p>



<


C ++程序中的结构创建,销毁,引用,访问和操作对象。对象是存储区域。


由于 new 存储,因此创建的任何东西都是一个对象,并且遵循规范的先例,可以称为实例。


What types in C++ can be instantiated?

I know that the following each directly create a single instance of Foo:

Foo bar;
Foo *bizz = new Foo();

However, what about with built-in types? Does the following create two instances of int, or is instance the wrong word to use and memory is just being allocated?

int bar2;
int *bizz2 = new int;

What about pointers? Did the above example create an int * instance, or just allocate memory for an int *?

Would using literals like 42 or 3.14 create an instance as well?

I've seen the argument that if you cannot subclass a type, it is not a class, and if it is not a class, it cannot be instantiated. Is this true?

解决方案

So long as we're talking about C++, the only authoritative source is the ISO standard. That doesn't ever use the word "instantiation" for anything but class and function templates.

It does, however, use the word "instance". For example:

An instance of each object with automatic storage duration (3.7.2) is associated with each entry into its block.

Note that in C++ parlance, an int lvalue is also an "object":

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.

Since new clearly creates regions of storage, anything thus created is an object, and, following the precedent of the specification, can be called an instance.

这篇关于什么可以实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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