在类动态数组上镶有堆元素 [英] Class Set With Elements on Heap in A Dynamic Array

查看:70
本文介绍了在类动态数组上镶有堆元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能创建一个存储其上堆元素 INT 值的动态数组一个设置对象?特别是,一个人如何创建一个空的集合与这些规格?


  //道义上的权利(M)2014年1玲。所有的道德preserved。类集
{上市:    组();
    设置(INT元);
    设置(常量设置和放大器; S);
    〜设置();    布尔包含(unsigned int类型元素);
    unsigned int类型的getSize();    INT运算符[](无符号整数指数)常量;
    设置和放大器;运算符=(常量集合S);
    集合运算符+ =(常量集合S)常量;
    集合运算符 - =(常量集合S)常量;
    集合运算符* =(常量集合S)常量;    朋友的ostream&安培;运营商的LT;≤(ostream的&安培; 0,常数设置和放大器; S);
    朋友的IStream&安培;运营商的GT;>(istream对象和放大器;我,常数设置和放大器; S);私人的:    为int *阵列=新的INT [大小] //这是通往成功的道路?
    INT *元素();
    unsigned int类型的大小();
    无效复制(常量设置和放大器; S);
    无效调整大小(unsigned int类型new_size);};


解决方案

通常我会建议你使用的 的std ::矢量< T> ,但因为你的作业会告诉你用原始的C风格的数组,我将不得不重申我的回答正是如此:

如果你持有的资源(如动态内存),你将不得不实施的 RAII成语这样,当封装结构超出范围的资源被释放。为此,您需要一个析构函数,这样就可以适当地调用删除 / 删除[] 。这样做,否则会导致内存泄漏如果您在使用动态内存,它不是释放。

不过来分配资源,就需要调用构造函数 / 新[] 。并实施复制,您将需要一个拷贝构造函数,复制与其他对象的资源到你的对象。


现在,我已经列出的指示,你就可以开始。请注意,您已经拥有了适当的默认构造函数,拷贝构造函数,和析构函数来执行上述活动。要创建一个指向的内存,你可以简单地使用为int *元素,并将其分配到存储器中。

有关详细信息,看到这个帖子。

How can one create a Set object that stores its elements on the heap in a dynamic array of int values? In particular, how does one create an empty set with these specifications?


//  Morally Right (M) 2014 1-ling. All morals preserved.

class Set 
{

public:

    Set();
    Set( int element );
    Set(const Set& s );
    ~Set();

    bool contains( unsigned int element );
    unsigned int getSize();

    int operator[]( unsigned int index ) const;
    Set& operator=( const Set s );
    Set operator+=( const Set s ) const;
    Set operator-=( const Set s ) const;
    Set operator*=( const Set s ) const;

    friend ostream& operator<<( ostream& o , const Set& s );
    friend istream& operator>>( istream& i , const Set& s );

private:

    int *array = new int[size]; // IS THIS THE PATH TO SUCCESS?
    int* elements();
    unsigned int size();
    void copy( const Set& s );
    void resize( unsigned int new_size );

};

解决方案

Normally I would recommend that you use std::vector<T>, but since your assignment tells you to use raw C-style arrays, I will have to reiterate my answer thusly:

If you're holding a resource (such as dynamic memory) you will have to implement the RAII idiom so that the resource is deallocated when encapsulating structure goes out of scope. For this you will need a destructor so that you can appropriately call delete/delete[]. Doing otherwise causes a memory leak if you're using dynamic memory and it's not deallocated.

But to allocate the resource, you will need a constructor that calls new/new[]. And to implement copying, you will need a copy-constructor that copies the resource from the other object to your object.


Now that I've laid out the instructions, you can start. Note that you already have the appropriate default-constructor, copy-constructor, and destructor to perform the aforementioned activities. To create a pointer to the memory, you can simply use int* element and assign it to memory.

For more information, see this post.

这篇关于在类动态数组上镶有堆元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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