C ++:自定义编码通用容器的潜在问题? [英] C++ : Potential Issues with Custom Coded Generic Container?

查看:112
本文介绍了C ++:自定义编码通用容器的潜在问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用STL和boost库,我必须在C ++中编写自己的容器。下面的代码在VC ++ 6中编译没有错误。

I am unable to use the STL and boost library and I have to write my own container in C++. The following code compiles without error in VC++6.

我没有实际测试过代码,但是关心这个通用容器是否将与原始类型和非原始类型(类)。复制构造函数和赋值运算符会有什么潜在的问题吗?

I have not actually tested the code but is concerned whether this generic container will work with both primitive and non primitive types (like class). Will there be any potential issues with the copy constructor and the assignment operator especially?

任何其他建议和意见是最受欢迎的。 :)

Any other suggestions and comments are most welcomed. :)

template <class T>  
class __declspec(dllexport) StdVector{

private:  
	int _pos;  
	int _size;  
	const T *_items;  

public:
	StdVector();
	StdVector(const StdVector &v);
	StdVector(int size);
	virtual ~StdVector();

	void Add(const T &item);
	void SetSize(int size);
	int GetSize();

	const T * Begin();
	const T * End();
	const T * ConstIterator();

	StdVector & operator=(const StdVector &v);
};

template <typename T>
StdVector<T>::StdVector() 
    : _pos(0), _size(0), _items(NULL){
}

template <typename T>
StdVector<T>::StdVector(const StdVector &v) 
    : _pos(0), _size(v.GetSize()), _items(new T[_size]){
    std::copy(v.Begin(), v.End(), Begin());
}

template <typename T>
StdVector<T>::StdVector(int size) 
    : _pos(0), _size(size), _items(new T[_size]){
}

template <typename T>
StdVector<T>::~StdVector(){
    if (_items != NULL)
	    delete[] _items;
}

template <typename T>
void StdVector<T>::Add(const T &item){
    if (_pos == _size)
	    throw new exception("Already at max size!!!");

    _items[_pos++] = item;
}

template <typename T>
void StdVector<T>::SetSize(int size){
    if (_items != NULL)
	    delete[] _items;

    _pos = 0;
    _size = size;
    _items = new T[_size];
}

template <typename T>
int StdVector<T>::GetSize(){
    return _size;
}

template <typename T>
const T * StdVector<T>::Begin(){
    return _items;
}

template <typename T>
const T * StdVector<T>::End(){
    return _items + _pos;
}

template <typename T>
const T * StdVector<T>::ConstIterator(){
    return _items;
}

template <typename T>
StdVector<T> & StdVector<T>::operator=(const StdVector &v){
    if (this != &v){
        delete[] _items;
        std::copy(v.Begin(), v.End(), Begin());
    }

    return *this;
}


推荐答案

并分配它们(如果 Begin()返回 T * ,而不是 const T * ,见dribeas的回答),如果你使用原始存储和构建新的对象就可能更有效。还有 GetSize() Begin() End()不是常数,它们不能在参数 v 上调用。

This default constructs the new objects and assigns them (or would, if the Begin() returned T* and not const T*, see dribeas' answer), it might be more efficient if you used raw storage and constructed the new objects in place. Also as GetSize(), Begin() and End() aren't const they can't be called on the parameter v.

template <typename T>
StdVector<T>::StdVector(const StdVector &v) 
    : _pos(0), _size(v.GetSize()), _items(new T[_size]){
    std::copy(v.Begin(), v.End(), Begin());
}

if语句是多余的。 delete [] 上一个NULL指针就好了。此外,有什么点是虚拟的?它看起来不像是设计为派生自的类。

The if statement is redundant. delete[] on a NULL pointer is fine. Also, is there any point to it being virtual? It doesn't look like this is a class designed to be derived from.

template <typename T>
StdVector<T>::~StdVector(){
    if (_items != NULL)
            delete[] _items;
}

SetSize销毁所有对象并创建新对象。这可能是令人惊讶的行为。此外,如果 new 抛出,对象将被指向释放的内存。再次,如果保护删除是多余的。

SetSize destroys all the objects and creates new ones. This may be 'surprising' behaviour. Also, if the new throws, the object will be left pointing at deallocated memory. Again, the if guarding the delete is redundant.

template <typename T>
void StdVector<T>::SetSize(int size){
    if (_items != NULL)
            delete[] _items;

    _pos = 0;
    _size = size;
    _items = new T[_size];
}

这是什么意思?它看起来像 Begin 一样。它甚至不是一个const方法。

What's the point of this? It appears to do the same as Begin. It's not even a const method.

template <typename T>
const T * StdVector<T>::ConstIterator(){
    return _items;
}



此尝试不会复制到 _items 刚刚被删除(再次参见 Begin()返回 const T * Begin() End()不是const)?

Doesn't this attempt copy to _items which has just been deleted (again see the points about Begin() returning const T* and about Begin() and End() not being const)?

template <typename T>
StdVector<T> & StdVector<T>::operator=(const StdVector &v){
    if (this != &v){
        delete[] _items;
        std::copy(v.Begin(), v.End(), Begin());
    }

    return *this;
}

例外这是? std :: exception 没有具有 const char * 的构造函数。您还应该抛出异常对象,而不是指向动态分配的异常对象的指针。清除被指针抛出的动态分配的异常几乎是不可能做到的。

What exception class is this? std::exception doesn't have a constructor that takes a const char*. You should also throw exception objects, not pointers to dynamically allocated exception objects. Cleaning up dynamically allocated exceptions which are 'thrown' by pointer is almost impossible to do robustly.

template <typename T>
void StdVector<T>::Add(const T &item){
    if (_pos == _size)
            throw new exception("Already at max size!!!");

    _items[_pos++] = item;
}

这篇关于C ++:自定义编码通用容器的潜在问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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