对象数组(类) [英] Array of object (class)

查看:59
本文介绍了对象数组(类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象(类)的数组.



...

array of object (class).



...

推荐答案

请自己完成-这很容易.您知道用C ++可以构建多少个非平凡的数据结构吗?如果您在所有情况下都要求提供示例,则您将没有时间学习任何东西.这样的答案对您没有帮助.

而且,顺便说一句,如果您的标准是简单",那么您最好立即放弃学习编程,而不是老实地浪费自己的时间.

—SA
Please do it by yourself — this is easy enough. Do you know how many non-trivial data structures can be built with C++? If you ask for examples in all cases, you won''t have any time to learn anything; and such answers would not help you.

And, by the way, if your criteria are like "is easy", you should better give up learning programming right now instead of wasting you life on it, honestly.

—SA


类数组与任何内置类型的数组没有区别.
除了之外,还可以使用[]运算符访问所需的元素.或->操作员访问成员函数/变量.

以字符串为例.

Arrays of classes are no different to arrays of any of the built-in types.
You use the [] operator to access the element you want in addition to the . or -> operators to access member functions/vars.

Take an example of strings.

#include <string>
#include <cstdio>
#include <cstdlib>

using std::string;

int main()
{
    int i;
    const int maxItems = 10;
    string someStrings[maxItems];

    for (i=0; i<maxItems; i++)
    {
        someStrings[i] = "TextItem: ";
        someStrings[i] += ('0' + i);
    }

    for (i=0; i<maxItems; i++)
        printf("String_%d is: %s\n", i, someStrings[i].c_str() );

    return 0;
}


数组是内存中包含相同大小的相同类型元素的区域.这些可以是对象或文字(整数,字符,浮点数,字符串,布尔值,指针或用户定义的文字).

数组符号a[n]是方便的语言构造,它提供对数组中偏移量n处的元素的访问.编译器知道每个元素的大小和对齐方式,因此您无需进行处理.
基本上,编译器将a[n]转换为内存中的地址.

类定义了对象的行为,因此我们可以拥有对象数组,而不是类,并且对象只是内存区域.

类可以提供关于数组运算符应如何工作的自己的想法,但通常建议在功能上在概念上等效于常规数组.

最好的问候
Espen Harlinn
An array is a region in memory containing equally sized elements of the same type. These can be objects or literals (integers, characters, floating point, string, boolean, pointer or user defined literals).

The array notation a[n] is convenient language construct that provides access to the element at offset n in the array. The compiler knows the size, and alignment, of each element so you don''t need to handle this.
Basically the compier translates a[n] into an address in memory.

A class defines the behaviour of an object, so we can have arrays of objects, but not classes, and an object is just a region of memory.

A class may provide it''s own idea about how the array operator should work, but it''s usually advisable that the functionality should be conceptually equivalent to a regular array.

Best regards
Espen Harlinn


这篇关于对象数组(类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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