我参加了高级 C++ 类(class)的测验,当我从以下问题中扣分时感到很惊讶:
在 C++ 中指定变量的四个属性。
我的回答:大小、类型、类、公共(public)
大小和类型被接受,但类和公共(public)不被接受。我的理解是类的实例是实例化变量的属性。并且在定义类时,成员变量可以是类的公有属性或私有(private)属性。
我问了我的助教和教授,他们声称我错了,因为实例变量与 int、double、char 等数据类型变量不同。
所以我的问题归结为:类的实例与原始类型变量的行为相同吗?为什么或者为什么不?
最佳答案
您的讲师有些正确。
您的导师可能正在寻找:
- 尺寸
- 类型
- 姓名
- 值(value)
还有其他属性,例如地址和链接,但您的教授不太可能指的是这些属性。
My understanding is that an instance of a class is an attribute of the instantiated variable
不,它(类)是变量的类型(这是一个属性,如上所述)
And when defining a class, the member variables can be public or private attributes of the class.
可见性仅适用于一小部分变量,对于成员变量,它们也可以在类中标记为protected
。
Do instances of a class act the same as primitive type variables? Why or why not?
我不明白你所说的“行动”是什么意思。如果您的意思是,可以将它们分配给它们,可以将它们传递给函数,它们是否使用相似的语法,那么可以。如果您指的是运营、他们的成员、他们的规模,那么就不是。
关于c++ - C++ 中变量的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48651811/